简体   繁体   中英

Why does the while loop keep looping in java

if I run these program sometimes it keeping running after print "Player win" or "Player lose" i can find why..

import java.security.SecureRandom;

public class craps {

    //create secure randowm number generator for use in method rolldice
    private static final SecureRandom RANDOMNUMBERS = new SecureRandom();

    // enum type with constants that represnet the game status
    private enum Status {
        CONTINUE, WON, LOST
    }

    // constanst that represent common rolls of the dice
    private static final int SNAKE_EYES = 2;
    private static final int TREY = 3;
    private static final int SEVEN = 7;
    private static final int YO_LEVEN = 11;
    private static final int BOS_CARS = 12;

    public static void main(String[] args) {

        int myPoint = 0;
        Status gameStatus;

        int sumOfDice = rollDice();

        switch (sumOfDice) {

            case SEVEN:
            case YO_LEVEN:
                gameStatus = Status.WON;
                break;
            case SNAKE_EYES:
            case TREY:
            case BOS_CARS:
                gameStatus = Status.LOST;
                break;
            default:
                gameStatus = Status.CONTINUE;
                myPoint = sumOfDice;
                System.err.printf("Point is %d\n", myPoint);
                break;
        }
        
        while (gameStatus == Status.CONTINUE){

             sumOfDice = rollDice();
        
             if(sumOfDice == myPoint)
                   gameStatus = Status.WON;
             else 
                 if (sumOfDice == SEVEN) 
                 gameStatus = Status.LOST;                 
        }
        
        if (gameStatus == Status.WON)
             System.err.println("Player Win ");
        else 
            System.err.println("Player Loses ");

        
        

    }

    public static int rollDice() {

        int dice1 = 1 + RANDOMNUMBERS.nextInt(6);
        int dice2 = 1 + RANDOMNUMBERS.nextInt(6);

        int sum = dice1 + dice2; // sum of die values

        //display results of this roll
        System.out.printf("Player roller %d  + %d = %d\n", dice1, dice2, sum);

        return sum;

    }

}

Here the output:

Player roller 3 + 2 = 5

Point is 5

Player roller 6 + 4 = 10

Player roller 6 + 6 = 12

Player roller 5 + 5 = 10

Player roller 1 + 2 = 3

Player roller 1 + 3 = 4

Player Win

Player roller 4 + 6 = 10

Player roller 4 + 1 = 5

BUILD SUCCESSFUL (total time: 0 seconds)

As you can tell from the provided output, your app is essentially working fine; it is not 'rolling too often': It is rolling exactly as often as it should, which is: Until the player rolls either 5 (the 'point' of this game) or 7, which.. it did; the last thing it shows is the 4+1 roll.

The actual 'problem' is that the Player Win text is being printed 'too early'.

What's actually going on is that System.out and System.err are different streams and there is no actual guarantee that these are synced up; if you print 'a' to System.out and immediately afterwards 'b' to System.err there is no guarantee that 'a' will always show before 'b' - these two streams are internally consistent though; if you print 'a' to System.out and immediately afterwards, 'b' to System.out , you are guaranteed that they always print in that order.

The fix is therefore quite simple: Pick one; either use only System.err , or only System.out .

Alternatively, synchronize on a lock anytime you print, but that's.. a lot of effort of some relatively advanced java.

NB: On the vast majority of platforms, System.out and System.err do end up being synced up or effectively synced up, you need some fairly specific setups to witness what you're seeing. Most folks won't be able to reproduce this.

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