简体   繁体   中英

System.out and System.err behave differently

I am a beginner in programming. As a part of my practice, I created a simple game, and I am having some problem in using out and err . The problem is described below with a portion of code and a screenshot of my output:

 do{
                System.out.print("Press 'r' to play, 'i' to get info and 'q' to quit: ");
                input = scan.next().charAt(0);
                switch (input){
                    case 'r':
                        roundCount++;
                        System.out.print("A random number is generated for you, guess what it is: ");
                        int answer = scan.nextInt();
                        allAnswers.add(answer);
                        randomNumber = getRandom();
                        if(isRightAnswer(answer,randomNumber)){
                            System.out.print("Congrats! that was absolutely right guess, the number was "+randomNumber + ", ");
                            scoreCount += 5;
                            rightAnswers.add(answer);
                        }else{
                            System.out.print("Oops!, wrong guess, right answer is "+randomNumber +", ");
                            wrongAnswers.add(answer);
                        }
                        break;
                    case 'q':
                        System.out.println("Bye Bye!");
                        break;
                    case 'i':
                        printInfo();
                        break;
                    default:
                        System.out.print("Wrong Input --> ");
                        break;
                }
            }while(input != 'q');

The problem is that both out and err behaves differently, as shown in the following two images, one output is generated by out and the other by err :

由 System.out 生成 System.out will print the line " Wrong input! -> " before the line " Press 'r' to play, 'i' to get info and 'q' to quit: ", which is exactly what I want

While System.err will print the line " Wrong input! -> " after the line " Press 'r' to play, as shown below: " 由 System.err 生成

What is the real reason behind this variety in the behavior of these two streams??

It looks like you use intelliJ witch seperates the err stream more then the normal command line output. It will color it red and do other debugging stuff. It can even happen that out lines executed after an err line will show up first. Only use the error stream for critical program malfunction not for wrong user actions. EDIT: even on the normal command line its not guaranteed that the two streams will show up in order, since you create a race condition about the command line window

Probably depend on the IDE you are using, I ran your code in my eclipse IDE using System.out and System.err and got the same outout for both. Probably in yours the out and err streams are being flushed differently.

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