Switch 语句,它不适用于提示 [英] Switch Statement, it does not work with prompt
本文介绍了Switch 语句,它不适用于提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚学习了 switch 语句.我通过建造一些东西来练习它.当我将变量的值设置为一个数字时,它可以工作,但是当我向用户询问一个数字时,它总是输出默认语句.
I just learned switch statements. I was practicing it by building something. When i set the value of variable to a number it works but when i asks the user for a number it always outputs the default statement.
它适用于以下代码:
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;
}
但是当我向用户询问价值时,它不起作用.下面的代码不起作用:
But when i asks value from user, it dosen't work. Code below doesn't work:
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;
}
推荐答案
您需要将用户输入从字符串转换为整数,就像这样
You need to convert the user input from a string to an integer, like so
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) {
//...
这篇关于Switch 语句,它不适用于提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文