我已经说过两个答案,一个是字符串,另一个是整数,试图使用带逻辑运算符的开关。这是我所拥有的,当我提供一个数字 1 时,它没有输出相关的答案。有没有办法实现这个目标?
如果以字符串或整数形式给出答案,如何接受答案?
我忘了添加这行代码
var test = prompt('your fav number ?');
user.toUpperCase();
switch(test) {
case 'one', 1:
var numberSentence = 'one';
var numbernumber = 1;
if(numberSentence === 'one' && numbernumber === 1) {
console.log('you got the answer') ;
}else {
console.log('pls try again');
}
break;
case 'two':
var twoSentence = 'two';
var twoNumber = 2;
if (twoSentence === 'two' || twoNumber === 2 ){
console.log('Cool you got it right');
}else {
console.log('do u want try again?');
}
console.log('it is two awesome' );
break;
case 'seven':
console.log('seven is my fav number as well ');
break;
default:
console.log('you could have at least said smth.');
}
答案 0 :(得分:3)
您不能在一个case
语句中放置两个值,但可以使用直通。如果你收到用户输入,它总是一个字符串,即使他们输入一个数字,所以你应该把它放在引号中。
test = prompt("Please type a number");
switch (test) {
case "1":
case "one":
alert("That's the first number");
break;
case "2":
case "two":
alert("Second place isn't as good");
break;
case "3":
case "three":
alert("Three is a magic number");
break;
default:
alert("I don't know that number");
}
如果case
未以break
结尾,则只会继续下一个案例。这允许您使用相同的代码处理多个案例。