【发布时间】: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