简体   繁体   English

switch语句default总是显示default

[英]Switch statement default always showing default

I have a switch statement. 我有一个switch语句。 It almost works fine, however instead of just showing one case, it shows the selected case then the default. 它几乎可以正常工作,但是它不仅显示一个案例,还显示所选案例,然后显示默认案例。 Here is my code: 这是我的代码:

 var people = { names: ["Sam", "Tim", "Steve"], emails: ["sam@email.com", "timm@messages.org", "stevieG@youhavemail.com"], phonenums: [1111, 2222, 4545] } var search = prompt("Type in someone's name to find their phone number and email."); switch (search) { case people.names[0]: alert(people.names[0] + "'s email: " + people.emails[0] + " phone number: " + people.phonenums[0]); case people.names[1]: alert(people.names[1] + "'s email: " + people.emails[1] + " phone number: " + people.phonenums[1]); case people.names[2]: alert(people.names[2] + "'s email: " + people.emails[2] + " phone number: " + people.phonenums[2]); default: alert("I don't know that person."); } 

Why does this happen? 为什么会这样?

Because you don't have any break in your switch cases. 因为您的开关盒没有任何break

Check the documentation for the switch statement on MDN . 查看文档中有关MDNswitch语句。 It says the following about break ( emphasis mine ) 它说以下关于break重点是我的

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. 与每个case标签关联的可选break语句可确保一旦执行了匹配的语句,程序就退出switch并在switch之后的语句处继续执行。 If break is omitted, the program continues execution at the next statement in the switch statement. 如果省略break,程序将在switch语句的下一个语句处继续执行。

So update your cases to look like 因此,将您的案件更新为

case people.names[0]:
  alert(people.names[0] + "'s email: " + people.emails[0] + " phone number: " + people.phonenums[0]);
  break;

您需要提供中断声明

You need to put breaks in a switch statement if you don't want the default to run. 如果您不希望默认值运行,则需要在switch语句中插入中断。 Just do this: 只要这样做:

var people = {
    names : ["Sam", "Tim", "Steve"],
    emails : ["sam@email.com", "timm@messages.org", "stevieG@youhavemail.com"],
    phonenums : [1111, 2222, 4545]
}

var search = prompt("Type in someone's name to find their phone number and email.");
switch(search) {
    case people.names[0]:
        alert(people.names[0] + "'s email: " + people.emails[0] + " phone number: " + people.phonenums[0]);
        break;

    case people.names[1]:
        alert(people.names[1] + "'s email: " + people.emails[1] + " phone number: " + people.phonenums[1]);
        break;

    case people.names[2]:
        alert(people.names[2] + "'s email: " + people.emails[2] + " phone number: " + people.phonenums[2]);
        break;

    default:
        alert("I don't know that person.");
}

In your particular scenario, you may be better off searching for the index of the correct person and using that so your array is free to grow and shrink. 在您的特定情况下,最好搜索正确的人的索引并使用该索引,这样您的数组就可以自由地增长和收缩。 Something like this could work: 这样的事情可能会起作用:

var search = prompt("Type in someone's name to find their phone number and email.");
boolean found = false;
int i = 0;
while(!found && i<people.names.length) {
    if(people.names[0] == search){
        found=true;
    } else {
        i++;
    }
}
if(found){
        alert(people.names[i] + "'s email: " + people.emails[i] + " phone number: " + people.phonenums[i]);
} else {
        alert("I don't know that person.");
}

My js is rusty and I'm using my phone, I'll check later for syntax errors if no one else spots any. 我的js生锈了,我正在使用手机,如果没有其他人发现,我将在以后检查语法错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM