简体   繁体   English

提示用户进行特定的输入,循环并提示直到输入正确的输入

[英]Prompt user for specific input, loop and prompt until correct input is entered

I am new here and currently learning JavaScript. 我是新来的,目前正在学习JavaScript。 In the code below, how can I get it to loop and still prompt if say for example the user does not enter 1 or -1? 在下面的代码中,如何使它循环并仍然提示,例如说用户未输入1或-1? Instead of using the else statement to output "This is not a valid input," I want it to prompt the user once again until they enter the desired input. 与其使用else语句输出“这不是有效的输入”,我希望它再次提示用户,直到他们输入所需的输入为止。 I am not quite getting the desired out with the while loop. 我不太希望通过while循环来实现。

var myShopping = ["Eggs", "Milk", "Potatoes", "Cereal", "Banana"];
var ord = prompt("Enter 1 for alphabetical order, " +
"and -1 for reverse order", 1);

if (ord == 1) {
    myShopping.sort();
    document.write(myShopping.join("<br />"));
} else if (ord == -1) {
    myShopping.sort();
    myShopping.reverse();
    document.write(myShopping.join("<br />"));
} else {
    document.write("That is not a valid input");
}

this is basically pseudo code, but should get you on the right track.. dont want to give you everything! 这基本上是伪代码,但是应该让您走上正确的路..不想给您一切! best way to learn is through trial and error :) 最好的学习方法是通过反复试验:)

var myShopping = ["Eggs", "Milk", "Potatoes", "Cereal", "Banana"];
while(true){
   //prompt here
   if(ord == 1){
       //do stuff
       break;
   }
   else if(ord == -1){
       //do stuff
       break;
   }
   else {
       //bad input
   }
}

we dont want to create the myShopping variable every time you go through the loop, so we put that outside of the loop... the prompt has to be inside incase they put in a bad character so that it will prompt them again. 我们不想在每次循环时都创建myShopping变量,因此我们将其放在循环之外...提示必须在内部,以防它们输入错误字符,以便再次提示它们。

some notes: document.write is pretty old and looks really bad so you should probably try to come up with a better way to do that... you may also want to put the logic of your if statements in a function you call so your code is neat 一些注意事项: document.write很老,看起来真的很糟糕,因此您可能应该尝试提出一种更好的方法...您可能还希望将if语句的逻辑放在调用的函数中,以便代码整洁

if you want to put it in a function then I would probably do something like this. 如果您想将其放在函数中,那么我可能会做这样的事情。

var doStuff = function(userInput){
    if(userInput == 1){
       //do stuff
       return true;
   }
   else if(userInput == -1){
       //do stuff
       return true;
   }
   else {
       //bad input
       return false;
   }
};

var myShopping = ["Eggs", "Milk", "Potatoes", "Cereal", "Banana"];
while(true){
    //prompt here
    var ord = prompt(... etc.);
    var toEndLoop = doStuff(ord);
    if(toEndLoop){
        break;
    } else {
        // they dun goofed.. tell them it was bad input and the prompt will happen again
    } 
}
var valid_ans = false;
while(!valid_ans) { // repeat this block until valid_ans is true
       // your code
       If (prompt===1 || prompt===-1) {
            valid_ans = true;
        }
  }

The above code should give you an idea. 上面的代码应该给您一个想法。 Keep in mind that your browser does not like multiple prompts. 请记住,您的浏览器不喜欢多个提示。 After two consecutive prompts the browser will ask the user if prompts should be suppressed. 在连续两次提示后,浏览器将询问用户是否应取消提示。

You could use a while loop like so: 您可以像这样使用while循环:

...
var promptText = "Your text";
var input = prompt(promptText);
while(input !== "1" && input !== "-1"){
  input = prompt("Your answer was not correct\n"+promptText);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM