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

时间:2013-11-18 16:13:21

标签: java

此代码是程序的一部分。

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.");

输出:

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.

为什么会这样?我尝试将它放在循环和语句之外(这可能是问题),但行为没有改变。我正在使用Eclipse。

1 个答案:

答案 0 :(得分:6)

这是两个不同的输出流,它们被调用的顺序并不是唯一重要的。他们可能会在不同的时间向控制台刷新内容。

编辑:经过多次挖掘this answer表示它可能是(未解决的)eclipse bug。它已经开放了10年,不会很快让你的希望得到修复。

相关问题