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

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

本文关键字:响应 无效 提示 prompt 验证      更新时间:2023-09-26

我想这样做,如果用户没有输入"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 loop:

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

您可以将代码放入函数中,如果不满足条件,则再次运行该函数:

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

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",因为它们在不同的作用域:)