简体   繁体   English

如何使用switch语句

[英]how to use switch statment

im new with JS and i read about the switch statement.我是 JS 新手,我阅读了 switch 语句。 i dont know how to use it i got an exercise to complete.我不知道如何使用它,我有一个练习要完成。 got an array with numbers 1-10 and the result need to be with words like "one","two","three".. Thats what i got so far :得到了一个数字为 1-10 的数组,结果需要包含“一”、“二”、“三”之类的词。这就是我到目前为止所得到的:

function sayNum(){
    let nameNumber = [1,2,3,4,5,6,7,8,9,10]
    let text = '';
    for(let i=0;i<nameNumber.length;i++){
    switch(numbers) {
        case "1":
            text = "one";
            break;
        case "2":
            text = "two";
            break;
        case "3":
            text = "three";
            break;
        case "4":
            text='four';
            break;
        case "5":
            text = "five";
            break;
        case "6":
            text = "six";
            break;
        case "7":
            text = "seven";
            break;
        case "8":
            text = "eight";
            break;
        case "9":
            text = "nine";
            break;
        case "10":
            text = "ten";
    }
    }
    return text;
}
sayNum()

You could do it like this, for example:你可以这样做,例如:

function sayNum(){
  let numbers = [1,2,3,4,5,6,7,8,9,10];
  let result = [];

  for(let i=0;i<numbers.length;i++) {
    switch(numbers[i]) {
        case 1:
            text = "one";
            break;
        case 2:
            text = "two";
            break;
        case 3:
            text = "three";
            break;
        case 4:
            text = "four";
            break;
        case 5:
            text = "five";
            break;
        case 6:
            text = "six";
            break;
        case 7:
            text = "seven";
            break;
        case 8:
            text = "eight";
            break;
        case 9:
            text = "nine";
            break;
        case 10:
            text = "ten";
            break;
    }

    result.push(text);
  }

  return result;
}

let namedNumbers = sayNum();
console.info(namedNumbers);

This will:这会:

  • Add the textual values to an array, representing each number in the source array将文本值添加到数组中,代表源数组中的每个数字
  • Return the resulting array to the caller将结果数组返回给调用者
  • Log the result in the consoleconsole记录结果

In the switch statement, you pass an 'expression' into the switch statement.在 switch 语句中,您将一个“表达式”传递到 switch 语句中。 The expression itself can be a string, a number, float, boolean etc. Now, the expression is compared to each of the case clause.表达式本身可以是字符串、数字、浮点数、布尔值等。现在,将表达式与每个 case 子句进行比较。 And this is a 'strict' comparison.这是一个“严格”的比较。 And that is the reason why your code was not working.这就是您的代码不起作用的原因。

Firstly you were passing an undeclared variable, 'numbers' as the switch expression.首先,您传递了一个未声明的变量,“数字”作为开关表达式。 Instead, you should be passing the ith element of the nameNumber[i] array like so: switch(nameNumber[i]){ }相反,您应该像这样传递nameNumber[i]数组的ith元素: switch(nameNumber[i]){ }

And, secondly, in each of your case clauses, you are comparing the value to a string like "1", "2".其次,在您的每个 case 子句中,您都将值与“1”、“2”等字符串进行比较。 But the switch expression is compared using strict equality === operator, therefore when your nameNumber array contains numbers and not strings, your 'case' clauses should also have numbers and not strings.但是 switch 表达式使用严格相等===运算符进行比较,因此当您的 nameNumber 数组包含数字而不是字符串时,您的“case”子句也应该包含数字而不是字符串。 That means case 1: instead of case "1": .这意味着case 1:而不是case "1":

You can read more about the switch-case here: Switch Statement您可以在此处阅读有关switch-case更多信息: Switch Statement

I have fixed your code below with the changes I mentioned above.我已经使用我上面提到的更改修复了下面的代码。 Please run the below code snippet to see how it works.请运行下面的代码片段,看看它是如何工作的。 Goodluck!祝你好运!

 function sayNum(){ let nameNumber = [1,2,3,4,5,6,7,8,9,10] let text = ''; for(let i=0;i<nameNumber.length;i++){ switch(nameNumber[i]) { case 1: text = "one"; break; case 2: text = "two"; break; case 3: text = "three"; break; case 4: text='four'; break; case 5: text = "five"; break; case 6: text = "six"; break; case 7: text = "seven"; break; case 8: text = "eight"; break; case 9: text = "nine"; break; case 10: text = "ten"; } console.log(text); } return text; } sayNum();

Rather than using a switch, use if statement.使用 if 语句,而不是使用 switch。 Because in switch, break statement can be used to jump out of a loop.因为在 switch 中,可以使用 break 语句跳出循环。

 let text = ''; function sayNum() { let nameNumber = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; for (let i = 0; i < nameNumber.length; i++) { if(nameNumber[i] == 1){ text += `"one",`; } if(nameNumber[i] == 2){ text += `"two",`; } if(nameNumber[i] == 3){ text += `"three",`; } if(nameNumber[i] == 4){ text += `"four",`; } if(nameNumber[i] == 5){ text += `"five",`; } if(nameNumber[i] == 6){ text += `"six",`; } if(nameNumber[i] == 7){ text += `"seven",`; } if(nameNumber[i] == 8){ text += `"eight",`; } if(nameNumber[i] == 9){ text += `"nine",`; } if(nameNumber[i] == 10){ text += `"ten"`; } } } sayNum(); console.log(text)

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

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