2 回答

TA貢獻1811條經驗 獲得超5個贊
這是一個小問題
如果您看到此功能,您將帳戶類型更改為'Savings' 或 'Checking'。
public void calculateNewBalance()
{
if (accountType.equals("S") || accountType.equals("s"))
{
accountType = "Savings";
calculateSavingsBalance();
} else if (accountType.equals("C") || accountType.equals("c"))
{
accountType = "Checking";
calculateCheckingBalance();
}
}
然而,您正在將 accountType 與'c' 或 's'進行比較
public void isServiceCharge()
{
if(accountType.equals("s") || accountType.equals("S"))
{
double newBalance = currentBalance - 10.0;
JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
+ "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
}
else if(accountType.equals("c") || accountType.equals("C"))
{
double newBalance = currentBalance - 25.0;
JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
+ "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
}
}
這就是它沒有進入任何一個塊的原因,因此,如果您將條件更改為以下語句
if(accountType.equals("Savings"))
else if(accountType.equals("Checking"))
它會按您的預期工作

TA貢獻1813條經驗 獲得超2個贊
你的代碼中實際發生了什么,
public void calculateNewBalance()
{
if (accountType.equals("S") || accountType.equals("s"))
{
accountType = "Savings";
calculateSavingsBalance();
} else if (accountType.equals("C") || accountType.equals("c"))
{
accountType = "Checking";
calculateCheckingBalance();
}
}
for s or S accountType = "Savings";
對于c 或 C accountType = "正在檢查";
但有趣的是在isServiceCharge()方法中
public void isServiceCharge()
{
if(accountType.equals("s") || accountType.equals("S"))
{
double newBalance = currentBalance - 10.0;
JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
+ "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
}
else if(accountType.equals("c") || accountType.equals("C"))
{
double newBalance = currentBalance - 25.0;
JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
+ "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
}
}
你檢查過
accountType.equals("s") || accountType.equals("S") //for savings
accountType.equals("C") || accountType.equals("c")// for checking
所以上述條件永遠不會滿足。
所以解決辦法是:
public void isServiceCharge()
{
if(accountType.equals("Savings"))
{
double newBalance = currentBalance - 10.0;
JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
+ "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
}
else if(accountType.equals("Checking"))
{
double newBalance = currentBalance - 25.0;
JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
+ "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
}
}
添加回答
舉報