首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaScript switch语句未执行case 1

JavaScript switch语句未执行case 1
EN

Stack Overflow用户
提问于 2018-06-20 05:49:56
回答 1查看 34关注 0票数 0

我正在尝试执行我的代码的第一种情况,但是由于某些原因没有发生,我对JS相当陌生,我使用w3来使用switch语句,但由于某种原因,它不会执行第一种情况,我甚至删除了断点

代码语言:javascript
复制
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 ");

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 05:58:33

它不会执行它,因为你没有返回任何东西给一个变量。

同样,使用布尔值也不是很正确,因为如果有人写了true或false以外的任何东西,它都不会起作用。

改为使用while循环进行检查。另外,如果您只是使用该代码,请尝试使用var而不是let。

你把你的情况设为0或1,这是没有意义的,因为你想要的是真或假。

试着这样做:

代码语言:javascript
复制
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");

还有一个额外的奖励提示,缩进你的代码,用空格或制表符,但请缩进它,不仅是为了我们,也是为了你。

祝你好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50937400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档