如何在javascript中的dowhile循环中嵌套if语句
How do you nest if statement in do-while loop in javascript
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)
请查看我的片段。由于某些原因,它不会运行,因为我已经检查了括号,并指出了一些括号错误。
其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);
dowhile循环语法如下:
示例
do {
}while(conditions);
因此,带有嵌套if/else语句的do-while循环如下所示:
do while w/nested if/elset
do{
if() {
}
else {
}
}while();
示例w/您的代码
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。:)
将的替换为do
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)
应该是
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);
相关文章:
- 嵌套if语句中的else语句.这段代码如何知道要执行哪个else语句
- 嵌套if的javascript问题
- Javascript/JQuery嵌套if语句工作不正常
- for循环中嵌套if条件的setTimeout
- 嵌套if语句vs&&操作员javascript jquery
- Javascript 多个嵌套 if 语句不适用于 Rails 代码
- 如何避免这样的嵌套if语句
- 多个嵌套if语句未进行计算
- 如何优化嵌套if
- Javascript嵌套If语句
- 如何避免混乱的嵌套if-else或开关
- 是否可以在手把模板中嵌套if/else语句
- 如何将带有嵌套if语句的if-else语句转换为一个三元语句
- Noobish JavaScript嵌套If Else语句帮助/建议/指针/YouNameIt
- 如何在Jquery Javascript中使用嵌套if else
- 我有一个嵌套if的函数.我怎样才能做出'这个'指向构造函数.父
- 较短的嵌套if语句
- 如何缩短我的嵌套if/else语句
- 在JavaScript中重构嵌套if语句
- 在javascript中嵌套if else