2016-09-23 81 views
1

我想這樣做,以便如果用戶沒有輸入「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 {}

+0

問題是什麼?你嘗試了什麼? – nanilab

+0

'prompt {choice1};'甚至沒有意義。你有沒有試過一個循環? – Xufox

+0

所以我做到了這一點,如果他們在提示中寫入1,它會觸發第一個選項,如果他們寫2它會觸發第二個,但如果他們寫3它什麼都不做,我怎麼做,所以他們不能寫任何東西除1或2以外,甚至取消或按OK –

回答

2

Working fiddle

使用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"); 

希望這有助於。

1

你可以把你的代碼中的函數,如果條件不滿足,再次運行功能:

function ask() { 
 
    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 { 
 
    ask(); 
 
    } 
 
} 
 
ask();

0
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."); 

while(choice != "1" && choice != "2") { 
    choice1 = prompt("-- You must choose either 1 or 2."); 
} 
if (choice1 == "1") { 
    alert("You start running into the woods. You stop, Out of breathe and realize you somehow got cut in your left arm."); 
} else { // choice1 must be "2" 
    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"); 
} 
+0

@splay您的意思是_triple equals_?不,如果'choice'是數字或字符串沒關係,所以double等於應該沒問題。 – David

0

首先關閉警報周圍的循環是完全不必要的,因爲它們每個只運行一次。但事實上,你設置他們,使他們只運行一次意味着你有一些循環,這是好奇的知識...

無論哪種方式來回答你的問題,你可以把這個代碼塊放在一個函數,說所謂的「提示」,然後在else塊中再次調用「提示」函數,這是一種稱爲遞歸的編程技術。不過說實話最好的解決辦法是使用while循環

while (choice1 !== "1" && choice1 !== "2") { 
    //prompt user for input 
} 

//Or even better imo 
while(true) { 
    //prompt user for input 
    if(choice1 === "1") { 
     // do your thing 
     break; // break out of the while loop 
    } 
} 

我提出,如果選擇1被包含在列表

while(!acceptedAnswers.contains(choice1)) 
可以通過創建一個硬編碼列表出來接受選項,看到改善的第一個解決方案

但即便如此,使用while(true)的第二種解決方案我認爲更易於閱讀和擴展。祝你好運!

哦,你可以同時擁有for循環具有相同的變量名,「我」在這種情況下,因爲它們是在不同的範圍:)

+0

'||'應該是'&&'。這是一個常見的錯誤,不應該在答案中出現。 – Barmar

+0

固定!謝謝 :) – splay