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

Sya*_*bam 2 java infinite-loop while-loop

我做了一些可以滚动五个骰子的东西,直到它变成五种骰子.我将在下面粘贴我的代码,以便您可以看到它.如果五种类型的成功,我已将我的while条件设置为false.在说"滚动......"后没有任何反应.我应该使用.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!");



    }


}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ica 5

你没有在循环中重新注册.如果roll不是0,它将是一个无限循环,打印"无效卷!" 一遍一遍又一遍.