脚本完成后,while循环不循环

时间:2020-10-05 16:59:31

标签: java loops while-loop

我有2个while循环,第一个while循环应该接收扫描器的输入并在周围循环。一旦脚本在第二个while循环中完成,我的代码就会停止。

while (timesLooped < loopTimes) {
  while (loss < 2) {

    Thread.sleep(10);

    Random randomGenerator1 = new Random();
    Random randomGenerator2 = new Random();
    int n = randomGenerator1.nextInt(max);

    int b = randomGenerator2.nextInt(max);
    if (n == 1 && b == 1) {
      loss = loss + 2;
      System.out.println("You got snake eyes and lost!");
    } else {
      score = score + n + b;
    }
    System.out.println("You got: " + n + b);
    System.out.println("Your new score is: " + score);

  }
  timesLooped = timesLooped + 1;
}

1 个答案:

答案 0 :(得分:0)

loss永远不会重置。一次完成内部while循环后,由于条件始终保持false,因此不再运行它。您添加其他打印语句以确认会发生这种情况:


while (timesLooped < loopTimes) {
    // Once this finishes loss remains the same
    while (loss > 2) {
        // Etc...
    }
    
    // Add a print statement so we can observe it running multiple times.
    System.out.println("Ran outer loop!");

    // You need to reset loss after running the loop
    // loss = 0;

    timesLooped = timesLooped + 1;
}