【问题标题】:System.out.println(); and System.err.println(); printing interchangeablySystem.out.println();和 System.err.println();可互换打印
【发布时间】:2013-11-18 16:13:21
【问题描述】:

此代码是程序的一部分。

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。

【问题讨论】:

    标签: java


    【解决方案1】:

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

    编辑:经过更多挖掘this answer 表明它也可能是(未解决的)eclipse bug。它已经开放了 10 年,不要对修复寄予厚望。

    【讨论】:

    猜你喜欢
    • 2012-05-16
    • 2010-12-25
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2010-12-05
    • 1970-01-01
    相关资源
    最近更新 更多