Do循环不断重复

时间:2018-11-22 17:43:18

标签: javascript

所以我的这段代码的替代版本是Java,虽然在JavaScript中,用户输入会无限重复,而不是随身携带直到用户迷路,但逻辑却非常相似。这是我的工作Java代码供参考:

int stop =0;
    Scanner scan = new Scanner(System.in);
    Random rand = new Random();

    do {
        int card;
        int upcommingcard;
        String userinput;

        card= rand.nextInt(13)+1;
        System.out.println("Card is "+card);
        System.out.println("Higher or Lower?");
        userinput = scan.next();
        upcommingcard = rand.nextInt(13)+1;

        if(!userinput.equalsIgnoreCase("H")&&(!userinput.equalsIgnoreCase("L"))){
            System.out.println("Invalid Input ");
        }
        else if((userinput.equalsIgnoreCase("H")) && (upcommingcard > card)){
            System.out.println("Correct!");
        }
        else if(userinput.equalsIgnoreCase("L") && upcommingcard < card){
            System.out.println("Correct!l ");
        }
        else {
            System.out.println("You lost it was " + upcommingcard);
            stop=1;

        }
    }while (stop != 1);
}

++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++ JavaScript-不起作用

var max=13;
var min=1;
var stop=0;
var card = Math.floor((Math.random() * (13 - 1) + 1));
var userinput = prompt("Card is "+card+"... Higher or lower?");
var upcommingcard = Math.floor((Math.random() * (13 - 1) + 1));
do{


    if((userinput !="H")&&(userinput !="L")){
        console.log("Invalid input");

    }
    else if((userinput ="H")&&(upcommingcard > card)){
        console.log("Correct!");

    }
    else if((userinput ="L")&&(upcommingcard < card)){
        console.log("Correct!");

    }
    else{
        console.log("You lost, it was "+ upcommingcard);
        stop=1;

}
}
while(stop !=1);

也要提及的是,它注册用户的输入是正确的,尽管它无法继续进行,并且只会不断吐出相同的输出,直到浏览器崩溃为止。

编辑:感谢您的答复!循环现在可以完美运行了,我唯一的问题是逻辑有些缺陷,因为有时我输入8的“ L”,而即将到来的int为10。

2 个答案:

答案 0 :(得分:0)

这不是您的控制台没有更新,而是您在输入不正确的情况下从不退出循环,也从未为他们提供重试的选项。

因此,如果它们不正确,循环将永远不会结束,控制台将不会更新,并且它们将无法重试。

我建议将代码更改为以下内容,以提醒用户重试。

var max = 13;
var min = 1;
var stop = 0;
var card = Math.floor((Math.random() * (13 - 1) + 1));
var userinput = prompt("Card is " + card + "... Higher or lower?");
var upcommingcard = Math.floor((Math.random() * (13 - 1) + 1));
do {


  if ((userinput != "H") && (userinput != "L")) {
    console.log("Invalid input");
    alert("Invalid input!");
    userinput = prompt("Card is " + card + "... Higher or lower?");

  } else if ((userinput == "H") && (upcommingcard > card)) {
    console.log("Correct!");
    alert("Correct!");
    stop = 1;

  } else if ((userinput == "L") && (upcommingcard < card)) {
    console.log("Correct!");
    alert("Correct!");
    stop = 1;

  } else {
    console.log("You lost, it was " + upcommingcard);
    alert("You lost, it was " + upcommingcard);
    stop = 1;

  }
}
while (stop != 1);

答案 1 :(得分:0)

关于这一点我想提出几点:

  1. 您的Java和Java代码逻辑不同。您在Java中do while内有变量,并且输入读取在Java中,但在Javascript之外。
  2. 由于您的prompt现在不在循环中,因此每次都会保持相同的输入值,而不会要求另一个,并且会一直持续到错误的猜测,或者如果输入无效就永远。接下来的问题使您的问题更糟:
  3. 您的if比较运算符无效。如评论中所述,您所做的是对userinput的数据分配,并且将始终返回正确的

话虽如此,但我在添加警报弹出窗口而不是仅console.log时在以下进行了更正:

var stop = 0;

do {
  var card = Math.floor((Math.random() * (13 - 1) + 1));
  var userinput = prompt("Card is " + card + "... Higher or lower?");
  var upcommingcard = Math.floor((Math.random() * (13 - 1) + 1));

  if ((userinput != "H") && (userinput != "L")) {
    console.log("Invalid input");
    alert("Invalid input");
    stop = 1; //Currently stopping if having invalid input, you can remove this later
  } else if ((userinput == "H") && (upcommingcard > card)) {
    //Note the '==' above, and also the next one for comparing equal values
    console.log("Correct!");
    alert("Correct");
  } else if ((userinput == "L") && (upcommingcard < card)) {
    console.log("Correct!");
    alert("Correct!");
  } else {
    console.log("You lost, it was " + upcommingcard);
    alert("You lost, it was " + upcommingcard);
    stop = 1;
  }
}
while (stop != 1);

现在,请将上面的JS代码段与您发布的有效Java代码进行比较。如果您再次将其与JS代码进行比较,那么您应该能够理解具有不同逻辑的含义。