如果 else 语句没有调用 isServiceCharge() 方法?

我几乎完成了这个银行账户程序。我有一个calculateSavings()方法或calculateCheckings()方法的调用。如果currentBalance小于要求的minSavingsor minChecking,我有一个else语句来调用该isServiceCharge()方法来扣除费用。该程序没有给我错误,但如果用户输入的 acurrentBalance小于minChecking或所需的数量minSavings,JOptionPane则不会显示输出。输入仍然有效,但没有弹出输出。其他一切都可以正常工作,例如增加利息,但在服务费中扣除费用则不然。我怎样才能解决这个问题?


主班


package com.company;


import javax.swing.*;


public class Main

{

    public static void main(String[] args)

    {

        {

            BankAccount myBank = new BankAccount();


            myBank.calculateNewBalance();

        }

    }

}


冉冉说
浏览 131回答 2
2回答

四季花海

这是一个小问题如果您看到此功能,您将帐户类型更改为'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"))它会按您的预期工作

慕姐8265434

你的代码中实际发生了什么,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 savingsaccountType.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);    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java