简体   繁体   中英

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. Nothing happens after it says "Rolling...". Should I be using .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!" 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.
  2. Second one is there is less possibility that the one to five variables going through the if condition.

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 .

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.

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

Thus when your while loop re-runs, it will notice the false condition and break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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