【问题标题】:Switch Statement, it does not work with promptSwitch 语句,它不适用于提示
【发布时间】:2015-10-18 04:29:52
【问题描述】:

我刚刚学习了 switch 语句。我正在通过构建一些东西来练习它。当我将变量的值设置为数字时,它可以工作,但是当我向用户询问数字时,它总是输出默认语句

它适用于以下代码:

confirm("You want to learn basic counting?");
var i = 0;
switch (i) {
    case 0:
        console.log(i);
        i++
    case 1:
        console.log(i);
        i++;
    case 2:
        console.log(i);
        i++;
    case 3:
        console.log(i);
        i++;
    case 4:
        console.log(i);
        i++;
    case 5:
        console.log(i);
        i++;
    case 6:
        console.log(i);
        i++;
    case 7:
        console.log(i);
        i++;
    case 8:
        console.log(i);
        i++;
    case 9:
        console.log(i);
        i++;
    case 10:
        console.log(i);
        console.log("Congratulations!");
        break;
    default:
        console.log("Buzz, wronghh");
        break;
}

但是当我向用户询问价值时,它不起作用。下面的代码不起作用:

confirm("You want to learn basic counting?");
var i = prompt("Type any number from where you want to start counting[Between 0 and 10]");
switch (i) {
    case 0:
        console.log(i);
        i++
    case 1:
        console.log(i);
        i++;
    case 2:
        console.log(i);
        i++;
    case 3:
        console.log(i);
        i++;
    case 4:
        console.log(i);
        i++;
    case 5:
        console.log(i);
        i++;
    case 6:
        console.log(i);
        i++;
    case 7:
        console.log(i);
        i++;
    case 8:
        console.log(i);
        i++;
    case 9:
        console.log(i);
        i++;
    case 10:
        console.log(i);
        console.log("Congratulations!");
        break;
    default:
        console.log("Buzz, wronghh");
        break;
}

【问题讨论】:

    标签: javascript syntax switch-statement prompt


    【解决方案1】:

    您需要将用户输入从字符串转换为整数,像这样

    confirm("You want to learn basic counting?");
    var i = prompt("Type any number from where you want to start counting[Between 0 and 10]");
    i = parseInt(i); // this makes it an integer
    switch(i) {
    //...
    

    【讨论】:

    • 谢谢!我不知道提示答案总是一个字符串。
    【解决方案2】:

    switch 语句在输入表达式和 case 表达式之间执行 strict comparison。以下输出将是:

    var i = 1;
    switch (i) {
        case "1":
            console.log('String 1');
            break;
        case 1:
            console.log('Number 1');
            break;
    }
    // Number 1
    
    var j = "1";
    switch (j) {
        case "1":
            console.log('String 1');
            break;
        case 1:
            console.log('Number 1');
            break;
    }
    // String 1
    

    提示函数返回一个字符串,因此:

    • 将您的案例陈述更改为case "1":case "2":
    • 使用i = Number(i) 区分用户输入的数字

    【讨论】:

      【解决方案3】:

      我知道这个问题之前已经回答过了,但我想补充一点。除了其他答案之外,您还可以使用一元加号 +。它实际上与 Number(...) 做同样的事情,但更短。 换句话说,加号运算符 + 应用于单个值,对数字没有任何作用。但如果操作数不是数字,一元加号会将其转换为数字。

      例如:

      let a = '2';
      alert( a + 3); // 23
      

      但是

      let a = '2';
      alert( +a + 3); // 5
      

      所以在代码中的提示符之前添加一元 +:

      var i = +prompt("Type any number from where you want to start counting[Between 0 and 10]");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多