【问题标题】:How do you nest if statement in do-while loop in javascript你如何在javascript的do-while循环中嵌套if语句
【发布时间】:2015-06-26 04:23:38
【问题描述】:

var temp = 110;
for {
    temp-=1
    if (temp >= 90) {
        console.log("Today's temperature is "+temp+"! "+"Lets go play ball")
    } else {
        console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!")
    }
    
}while (temp > 90)

请查看我的 sn-p。由于我已经检查了括号,由于某种原因它不会运行说明一些括号错误。

【问题讨论】:

  • 你用错了。替换它来做。
  • do替换for
  • 我有一个问题要问你!如果您要求用户提示,则不应使用 while 循环,因为这将使您的循环永远持续下去。
  • 我明白了。对不起我的无知。我只是想看看如何实现它......迟早我会自己意识到让用户提示代码是一个错误。 =\谢谢你的建议!

标签: javascript loops if-statement


【解决方案1】:

它的do 不是for

var temp = 110;
do { //its `do` not `for`
  temp -= 1;
  if (temp >= 90) {
    console.log("Today's temperature is " + temp + "! " + "Lets go play ball")
  } else {
    console.log("Today's temperature is " + temp + "! " + "It is too hot today to ball!")
  }

} while (temp > 90);

您可以使用prompt() 进行用户输入:

var temp = prompt('Input temperature', '110'); // (message, default input)
console.log('temp', temp);
do { //its `do` not `for`
  temp -= 1;
  if (temp >= 90) {
    console.log("Today's temperature is " + temp + "! " + "Lets go play ball")
  } else {
    console.log("Today's temperature is " + temp + "! " + "It is too hot today to ball!")
  }

} while (temp > 90);

【讨论】:

  • 谢谢!现在有没有办法只显示一个温度提示(ing)用户温度?
  • 感谢您的匆忙更新!有没有办法只控制.记录提示温度?
【解决方案2】:

do while 循环的语法是这样的:

示例

do {

}while(conditions);

所以带有嵌套 if/else 语句的 do-while 循环是这样的:

do-while w/嵌套 if/else

do{

if() {
}
else {
}
}while();

带有您的代码的示例

var 温度 = 110;

do {
 if(temp >= 90) 
{
    console.log("Today's temperature is "+temp+"! "+"Lets go play ball");
{
else 
{
    console.log("Today's temperature is "+temp+"! "+"It is too hot today to play      ball!");
}
 temp--; 
} while(temp > 90);

好的,现在让我解释一下这里发生了什么。您实际上在做的是告诉编译器做某事,直到 while 循环返回 true。因此,如果您注意到我更改了temp -= 1; to temp--;,这完全一样,只是使用后者更加标准。实际上,您与原始代码非常接近,除了它是 do-while 循环而不是 for-while。 :)

【讨论】:

  • 非常感谢您的详细解释!如果有办法修改只显示用户输入的指定临时的console.log的代码?
  • 是的,只是输出用户输入。 var sample = prompt("Input Temp");然后像这样打印出用户输入,console.log(sample);
  • 确保将其标记为答案,它可以帮助我和其他人! :)
  • 你到底想做什么?这样我就可以写出特定于你正在尝试做的代码。
  • 我想console.log if 语句中的一个或另一个。但是,它显示了console.log 的所有迭代。然后我意识到它应该显示,因为它是在循环函数中完成的。问题已解决,感谢您的宝贵时间!
【解决方案3】:

用做替换。像这样

var temp = 110;
do{
    temp-=1
    if (temp >= 90) {
        console.log("Today's temperature is "+temp+"! "+"Lets go play ball")
    } else {
        console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!")
    }

}while (temp > 90)

【讨论】:

  • 谢谢!现在有没有办法只显示一个温度提示(ing)用户温度?
【解决方案4】:

应该是

var temp = 110;
do {
    temp-=1
    if (temp >= 90) {
        console.log("Today's temperature is "+temp+"! "+"Lets go play ball")
    } else {
        console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!")
    }
    
}while (temp > 90);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多