简体   繁体   English

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

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

I've made something that rolls five dice until it gets a five-of-a-kind. 我做了一些可以掷五个骰子的东西,直到获得五个骰子。 I will paste my code below so you can see it. 我将在下面粘贴我的代码,以便您可以看到它。 I HAVE set my while condition to false if the five-of-a-kind is successful. 如果成功五次,我将while条件设置为false。 Nothing happens after it says "Rolling...". 说“正在滚动...”之后,什么都没有发生。 Should I be using .equals()? 我应该使用.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!");



    }


}

You're not rerolling inside the loop. 您无需在循环中重新滚动。 If roll isn't 0, it'll be an infinite loop, printing "Invalid roll!" 如果roll不为0,则将是一个无限循环,并显示“ Invalid roll!”。 over and over and over. 一遍一遍又一遍。

There is few things that can lead to infinite loop here 几乎没有什么东西可以导致无限循环

  1. First one is your roll variable always zero you are not rolling again or not changing that variable. 第一个是您的roll变量始终为零,您不会再次滚动或不更改该变量。
  2. Second one is there is less possibility that the one to five variables going through the if condition. 第二个原因是五个变量通过if条件的可能性较小。

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

Also you do not need to use while(rolling == true) instead you can use while(rolling) because your rolling variable initialized as true . 另外,您不需要使用while(rolling == true)而可以使用while(rolling)因为您的rolling变量已初始化为true

Here is what I came up with 这是我想出的

            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!");

You need to change rolling to false if roll is not 0. 如果roll不为0,则需要将rolling更改为false。

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

Thus when your while loop re-runs, it will notice the false condition and break. 因此,当您的while循环重新运行时,它将注意到false情况并中断。

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

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