我想這樣做,以便如果用戶沒有輸入「1」或「2」,則必須重新回答問題。我試過prompt{choice1};
但它不起作用。驗證來自prompt()的響應。對無效回覆再次提示
任何解決方案?
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 {}
問題是什麼?你嘗試了什麼? – nanilab
'prompt {choice1};'甚至沒有意義。你有沒有試過一個循環? – Xufox
所以我做到了這一點,如果他們在提示中寫入1,它會觸發第一個選項,如果他們寫2它會觸發第二個,但如果他們寫3它什麼都不做,我怎麼做,所以他們不能寫任何東西除1或2以外,甚至取消或按OK –