我正在尝试执行我的代码的第一种情况,但是由于某些原因没有发生,我对JS相当陌生,我使用w3来使用switch语句,但由于某种原因,它不会执行第一种情况,我甚至删除了断点
let theGreeting = String(prompt("enter your name"));
console.log("hello " + theGreeting + " " + "how are you I hope well");
let theQuestion = Boolean(prompt("enter boolean value "));
switch (theQuestion) {
case 0:
theQuestion == false;
console.log("it was" + theQuestion + "that is very good to hear that you were not sick ");
break;
case 1:
theQuestion == true;
console.log("sorry to hear that it is " + theQuestion + " " + "that you were very sick ");
break;
};
let theNumber = Number(prompt("enter number"));
console.log("I am sorry to hear that I am surprised that the treatment was" +
" " + theNumber * theNumber + " " + " dollars " + "thank god for insurance ");
发布于 2018-06-20 05:58:33
它不会执行它,因为你没有返回任何东西给一个变量。
同样,使用布尔值也不是很正确,因为如果有人写了true或false以外的任何东西,它都不会起作用。
改为使用while循环进行检查。另外,如果您只是使用该代码,请尝试使用var而不是let。
你把你的情况设为0或1,这是没有意义的,因为你想要的是真或假。
试着这样做:
let theGreeting = String(prompt("enter your name"));
console.log("hello " + theGreeting + " " + "how are you I hope well");
let theQuestion = Boolean(prompt("enter boolean value "));
switch (theQuestion) {
case true:
theQuestion = false;
console.log("it was" + theQuestion + "that is very good to hear that you were
not sick");
break;
case false:
theQuestion = true;
console.log("sorry to hear that it is " + theQuestion + " " + "that you were
very sick");
break;
};
let theNumber = Number(prompt("enter number"));
console.log("I am sorry to hear that I am surprised that the treatment was"
+ " " + theNumber * theNumber + " " + " dollars " + "thank god for
insurance");
还有一个额外的奖励提示,缩进你的代码,用空格或制表符,但请缩进它,不仅是为了我们,也是为了你。
祝你好运!
https://stackoverflow.com/questions/50937400
复制相似问题