我想这样做,如果用户不输入“1”或“2”,则必须重新回答问题。我尝试了 prompt{choice1};
但它不起作用。
有什么解决办法吗?
var choice1 = prompt("You see a bear on your campsite, What do you do ? Type 1 if you start running into the woods or type 2 if you fight the bear.");
if (choice1 == "1") {
for (var i = 2; i < 3; i++) {
alert("You start running into the woods. You stop, Out of breathe and realize you somehow got cut in your left arm.");
}
} else if (choice1 == "2") {
for (var b = 0; b < 1; b++) {
alert("You look around you to find something that could help you fight off the bear. You see a rock and you pick it up. The bear is getting ready to attack and right away you throw the rock");
}
} else {}
最佳答案
使用 do..while
循环:
do {
var choice1 = prompt("You see a bear on your campsite, What do you do ? Type 1 if you start running into the woods or type 2 if you fight the bear.");
if (choice1 == "1") {
for (var i = 2; i < 3; i++) {
alert("You start running into the woods. You stop, Out of breathe and realize you somehow got cut in your left arm.");
}
} else if (choice1 == "2") {
for (var b = 0; b < 1; b++) {
alert("You look around you to find something that could help you fight off the bear. You see a rock and you pick it up. The bear is getting ready to attack and right away you throw the rock");
}
}
}
while (choice1 != "1" && choice1 != "2");
希望这对您有所帮助。
关于javascript - 验证来自 prompt() 的响应。再次提示无效响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39670609/