简体   繁体   English

的System.out.println();和System.err.println();可互换印刷

[英]System.out.println(); and System.err.println(); printing interchangeably

This code is part of a program. 此代码是程序的一部分。

System.out.print("Enter your deposit amount: ");
double deposit = scanner.nextDouble();
bankObj.deposit(deposit); //this method will print a syserr if the number entered is negative
if (deposit > 0) {
    System.out.println("Thank you for depositing the amount of "+deposit+" to account number "+bankObj.accountNum+".");
}
    System.out.println("Program exiting.");

Output: 输出:

Enter your deposit amount: -2
//sometimes it will print.
Program exiting.
Account.deposit(...): cannot deposit negative amount. //This is from the deposit method
//or
Account.deposit(...): cannot deposit negative amount. //This is from the deposit method
Program exiting.

Why is this happening? 为什么会这样? I tried putting it outside loops and statements (those might be the problem) but the behavior didn't change. 我尝试将它放在循环和语句之外(这可能是问题),但行为没有改变。 I'm using Eclipse. 我正在使用Eclipse。

Those are two different output streams, the order in which they are invoked is not the only thing that matters. 这是两个不同的输出流,它们被调用的顺序并不是唯一重要的。 They might flush their contents towards the console at a different time. 他们可能会在不同的时间向控制台刷新内容。

Edit: after a bit more digging this answer indicates that it might be an (unresolved) eclipse bug as well. 编辑:经过多一点挖掘后, 这个答案表明它可能是一个(未解决的) 日食错误 It's been open for 10 years, don't get your hopes up on a fix anytime soon. 它已经开放了10年,不会很快得到你的希望。

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

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