我是一个初学者,试图用JavaScript制作文字冒险游戏,我需要重复switch语句,直到用户输入有效答案为止:

opts = prompt("Do you want to TALK or LOOK?").toUpperCase();

switch(opts) {
  case "TALK":
    mes = "Is anyone in charge here?";
    speech = "Our leader is in the big building.";
    talkNot();
    break;
  case "LOOK":
    window.alert("The buildings are quite simple, and the doorways are much shorter than you. You notice, however, that there is a very tall, thin building at the end of the street.");
    break;
  default:
    window.alert("That's not an option.");
}


任何答案都将非常有帮助-谢谢!

最佳答案

用一些函数包装代码,并在默认语句中用callback函数

function run(){
opts = prompt("Do you want to TALK or LOOK?").toUpperCase();

switch(opts) {
  case "TALK":
    mes = "Is anyone in charge here?";
    speech = "Our leader is in the big building.";
    console.log('talk')
    //talkNot();
    break;
  case "LOOK":
    window.alert("The buildings are quite simple, and the doorways are much shorter than you. You notice, however, that there is a very tall, thin building at the end of the street.");
    break;
  default:
    window.alert("That's not an option.");
    run() //callback
}
}

run()

10-06 08:04