So I am trying to create little text based game. The problem is that when someone makes a spelling mistake, it will always end the game via the "default" clause where I had to put an alert. Is there any way to make it go back to caveAnswer prompt when someone type incorrect answer?
所以我试图创建一个基于文本的游戏。问题是,当有人犯拼写错误时,它总是会通过我必须发出警报的“默认”条款结束游戏。当有人输入错误答案时,有没有办法让它回到caveAnswer提示?
var caveAnswer = prompt("you are in the cave", "type GO or EXIT").toUpperCase();
switch(caveAnswer){
case "GO":
prompt("some text...", "type blabla");
break;
case "EXIT":
alert("COWARD! HAHAHA!");
break;
default:
alert('I dont understand ' + caveAnswer);
break;
}
1 个解决方案
#1
0
Wrap in inside a function and have the default
case call itself to show the prompt again.
包含在函数内部并具有默认大小写调用本身以再次显示提示。
function ask() {
var caveAnswer = prompt("you are in the cave", "type GO or EXIT").toUpperCase();
switch(caveAnswer){
case "GO":
prompt("some text...", "type blabla");
break;
case "EXIT":
alert("COWARD! HAHAHA!");
break;
default:
alert('I dont understand ' + caveAnswer);
ask();
break;
}
}
ask();
#1
0
Wrap in inside a function and have the default
case call itself to show the prompt again.
包含在函数内部并具有默认大小写调用本身以再次显示提示。
function ask() {
var caveAnswer = prompt("you are in the cave", "type GO or EXIT").toUpperCase();
switch(caveAnswer){
case "GO":
prompt("some text...", "type blabla");
break;
case "EXIT":
alert("COWARD! HAHAHA!");
break;
default:
alert('I dont understand ' + caveAnswer);
ask();
break;
}
}
ask();