验证来自 prompt() 的响应。再次提示无效响应

Validate response from prompt(). Prompt again on invalid response

我想这样做,如果用户不输入“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 {}

您可以将代码放在一个函数中,如果不满足条件,运行 再次调用该函数:

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();

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

希望对您有所帮助。

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");
}

首先,您的警报循环是完全没有必要的,因为它们每个 运行 只有一次。但事实上,您将它们设置为仅 运行 一次意味着您对循环有一些了解,这很奇怪...

无论哪种方式来回答你的问题,你都可以将这个代码块放在一个函数中,比如称为 "prompt" 然后在 else 块中再次调用 "prompt" 函数这是一种称为递归的编程技术.但老实说,最好的解决方案是使用 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 
    }
}

我提出的第一个解决方案可以通过创建一个硬编码的接受选项列表并查看列表中是否包含 choice1 来改进

while(!acceptedAnswers.contains(choice1)) 

但即便如此,我认为使用 while(true) 的第二种解决方案更易于阅读和扩展。祝你好运!

哦,您可以让两个 for 循环具有相同的变量名,在本例中为 "i",因为它们在不同的范围内 :)