我正在尝试编写一个BankAccount.java类,除了提取方法外我几乎完成了。我留下了解释我对所写代码的理解的评论意见,但我无法找到导致逻辑错误的原因。似乎无法找到正确的if语句或返回
测试程序的帐户1余额= 100和帐户2余额= 200 提款金额是帐户1 = 100和帐户2 = 201所以显然100应该跟随如果返回和帐户2应遵循其他回报但是,这不是案件。
public int withdraw(double amount) {
AccountBalance = this.AccountBalance - amount; // Setting AccountBalance to what it would be when the withdraw amount is subtracted
if (0 < AccountBalance) // testing if 0 is less than AccountBalance
return (int)AccountBalance; // if so then i return the new positive balance
else
return (int)AccountBalance + (int)amount; //if less then 0 i return to what the balance was before withdraw was subtracted because i can't have a negative balance
}
这是以下输出我在CMD接收:
F:\>java BankDemo
The balance of account1 is: 100.0
The balance of account2 is: 200.0
account1 information: John Smith
AccountNumber: 12345
Balance: 0.0
account2 information: John Calipari
AccountNumber: 54321
Balance: -1.0
上述两个帐户是相同?假
最后,虽然我有可能是这个错误是不是在这种方法和在整个班级不同的方法。我不认为这是事实,因为我有三重检查,但如果这似乎是一种可能性反馈会很好,因为我会把焦点从这种方法转到我班的其他人。
你为什么要投射到'int'?我认为我们可能需要查看更多的代码。 – voidHead
您是否将'AccountBalance'设置为您从此方法返回的金额?如果不是,那么你如何使用返回值?在“否定”情况下,这种方法本身会将“AccountBalance”视为负数。 –