验证来自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 {}
工作小提琴
使用 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",因为它们在不同的作用域:)
相关文章:
- Laravel数据表无效的JSON响应
- AJAX:无效的JSON响应
- IPV4地址的Regex给出无效响应
- 预检响应具有无效的 HTTP 状态代码 405 - AngularJS + Spring MVC
- 来自 Google Identity Toolkit 的 IDP 响应无效
- XMLHttpRequest 无法加载 [url].预检响应无效(重定向)
- Chrome MJPEG CORS “无效响应” 当 img.crossOrigin=“Anonymous” 时
- 为什么 AJAX 对第一个函数的响应“_blank”有效,但对第二个函数无效
- 如果对 JSONP 请求的服务器响应未正确包装在函数中,是否有任何方法可以覆盖“无效标签”错误
- XMLHttpRequest无法加载飞行前的url响应具有无效的HTTP状态代码404
- XMLHttpRequest无法加载飞行前的[url]响应具有无效的HTTP状态代码405
- 来自IE9的AJAX响应包含无效字符
- XMLHttpRequest无法加载URL.飞行前的响应具有无效的HTTP状态代码404
- codeigniter php中的Json响应无效
- Json响应无效,Json有效
- 在IE11中设置xmlHttp响应类型为msxml-document会抛出无效状态错误
- 在reset()之后无效的响应表达式
- 使用Microsoft图形API更新任务,给出400个无效请求的响应
- 服务器返回了一个无效的响应
- 验证来自prompt()的响应.无效响应再次提示