简体   繁体   English

while循环为什么一直循环到java

[英]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..如果我有时运行这些程序,它会在打印“Player win”或“Player lose”后继续运行,我可以找到原因..

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:这里是 output:

Player roller 3 + 2 = 5玩家滚筒 3 + 2 = 5

Point is 5点数为 5

Player roller 6 + 4 = 10玩家滚筒 6 + 4 = 10

Player roller 6 + 6 = 12玩家滚筒 6 + 6 = 12

Player roller 5 + 5 = 10玩家滚轮 5 + 5 = 10

Player roller 1 + 2 = 3玩家滚筒 1 + 2 = 3

Player roller 1 + 3 = 4玩家滚筒 1 + 3 = 4

Player Win玩家获胜

Player roller 4 + 6 = 10玩家滚筒 4 + 6 = 10

Player roller 4 + 1 = 5玩家滚筒 4 + 1 = 5

BUILD SUCCESSFUL (total time: 0 seconds)构建成功(总时间:0 秒)

As you can tell from the provided output, your app is essentially working fine;从提供的 output 可以看出,您的应用基本上运行良好; 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;不是“滚动得太频繁”:它滚动的频率与它应该的频率完全一样,即:直到玩家掷出 5(该游戏的“点数”)或 7,它......它做到了; the last thing it shows is the 4+1 roll.它显示的最后一件事是 4+1 掷骰。

The actual 'problem' is that the Player Win text is being printed 'too early'.实际的“问题”是Player Win文本打印“太早”。

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;实际发生的是System.outSystem.err是不同的流,并且不能实际保证它们同步; 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;如果您将“a”打印到System.out ,然后立即将“b”打印到System.err ,则无法保证“a”始终显示在“b”之前——尽管这两个流在内部是一致的; if you print 'a' to System.out and immediately afterwards, 'b' to System.out , you are guaranteed that they always print in that order.如果您将 'a' 打印到System.out ,然后立即将 'b' 打印到System.out ,则可以保证它们始终按该顺序打印。

The fix is therefore quite simple: Pick one;因此,修复非常简单:选择一个; either use only System.err , or only System.out .要么只使用System.err ,要么只使用System.out

Alternatively, synchronize on a lock anytime you print, but that's.. a lot of effort of some relatively advanced java.或者,在你打印的任何时候同步锁,但这是......一些相对先进的 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.注意:在绝大多数平台上, System.outSystem.err最终会同步或有效同步,您需要一些相当具体的设置来见证您所看到的。 Most folks won't be able to reproduce this.大多数人将无法重现这一点。

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

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