【发布时间】:2016-08-25 04:10:47
【问题描述】:
我为家庭作业创建了一个非常基本的银行账户程序,但我一直遇到逻辑错误。而不是程序在存款、取款和加息后给出总余额,它只输出存款 - 取款的金额。感谢您的帮助,谢谢!
public class BankAccount
{
public BankAccount(double initBalance, double initInterest)
{
balance = 0;
interest = 0;
}
public void deposit(double amtDep)
{
balance = balance + amtDep;
}
public void withdraw(double amtWd)
{
balance = balance - amtWd;
}
public void addInterest()
{
balance = balance + balance * interest;
}
public double checkBal()
{
return balance;
}
private double balance;
private double interest;
}
测试类
public class BankTester
{
public static void main(String[] args)
{
BankAccount account1 = new BankAccount(500, .01);
account1.deposit(100);
account1.withdraw(50);
account1.addInterest();
System.out.println(account1.checkBal());
//Outputs 50 instead of 555.5
}
}
【问题讨论】:
-
您没有正确初始化变量。你应该有
balance=initBalance ; interest=initInterest。 -
没有解释的否决票是没有帮助的。他们还可以阻止新用户询问和寻求帮助或建议。我认为应该尽可能避免新用户的否决投票问题。对于那些我建议相反的人:解释而不投反对票。