【发布时间】:2022-08-02 20:30:24
【问题描述】:
我是 javascript 的初学者。我正在为我的程序编写一些代码,其中主要是它的循环。这是关于你输入你的年龄,它说你应该开车还是不开车,但是,我想让它重复自己,直到用户没有输入 n 或 N。我写了一个带有 break 和另一个的 if 语句else - 带有 continue 的 if 语句。当我按 n 时它会停止,但是当我输入 y 时它不会继续。请尝试帮助我这是下面的代码:
while(true){
let age = prompt(\"Enter your age\")
if (age>18){
console.log(\"You can drive\")
}
else if (age<0){
console.log(\"Invalid age\")
}
else if (age>100){
console.log(\"Invalid age\")
}
else if (age==18){
console.log(\"Come to our office\")
}
else{
console.log(\"You cannot drive\")
}
let choice = prompt(\"Type y or Y to run again type n or N to exit\")
if(choice == \"n\" || \"N\"){
continue
}
if (choice == \"y\" || \"Y\")
break
}
-
continue从头开始再次循环 -break退出循环。你让他们落后了!
标签: javascript loops if-statement while-loop