Java while 循环不会中断(我也将条件设置为 false)

Java while loop won't break (I've set my condition to false, too)

我做了一些东西,可以掷五个骰子,直到它变成五颗骰子。我将在下面粘贴我的代码,以便您可以看到它。如果同类五项成功,我已将我的 while 条件设置为 false。在它说 "Rolling..." 之后没有任何反应。我应该使用 .equals() 吗?

package omega;

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class One {

    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        Scanner scan = new Scanner(System.in);

        int one = 1+random.nextInt(6);
        int two = 1+random.nextInt(6);
        int three = 1+random.nextInt(6);
        int four = 1+random.nextInt(6);
        int five = 1+random.nextInt(6);

        boolean rolling = true;
        int rollCount = 0;

        System.out.println("====================");
        System.out.println("      Yahtzee!      ");
        System.out.println("====================");
        System.out.println("");


            System.out.println("Type 0 to roll.");
            int roll = scan.nextInt();

            System.out.println("Rolling...");

            while(rolling == true) {
                switch(roll) {
                case 0 :
                    if(two == one && three == one && four == one) {
                        rollCount++;
                        rolling = false;
                    }
                    else {
                        rollCount++;
                    }
                    break;
                default :
                    System.out.println("Invalid roll!");
                }
            }

            System.out.println("Yahtzee!");
            System.out.println("Took " + rollCount + " rolls!");



    }


}

您没有在循环中重新滚动。如果 roll 不是 0,它将是一个无限循环,一遍又一遍地打印 "Invalid roll!"。

这里几乎没有什么东西可以导致无限循环

  1. 第一个是你的roll变量总是你不是 再次滚动或不更改该变量。
  2. 第二个是一个五个变量的可能性较小 经历 if 条件。

if(two == one && three == one && four == one) { rollCount++; rolling = false; }

您也不需要使用 while(rolling == true) 而是可以使用 while(rolling) 因为您的滚动变量初始化为 true

这是我想出的

            while(rolling) {
                switch(roll) {
                case 0 :
                    if(two == one && three == one && four == one) {
                        rollCount++;
                        rolling = false;
                    }
                    else {
                        rollCount++;
                        System.out.println("If you want to stop roll press -1: ");
                        System.out.println("Or type 0 to roll again.");
                        roll = scan.nextInt();

                        if(roll == -1){
                            System.out.println("Yahtzee!");
                            System.out.println("Took " + rollCount + " rolls!");
                            return;
                        }
                    }
                    break;
                default :
                    System.out.println("Invalid roll!");
                    roll = 0;
                }
            }

            System.out.println("Yahtzee!");
            System.out.println("Took " + rollCount + " rolls!");

如果 roll 不为 0,您需要将 rolling 更改为 false。

default :
    System.out.println("Invalid roll!");
    rolling = false;      

因此,当您的 while 循环重新运行时,它会注意到 false 条件并中断。