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

If else statement not calling isServiceCharge() method?

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

主要class

package com.company;

import javax.swing.*;

public class Main
{
    public static void main(String[] args)
    {
        {
            BankAccount myBank = new BankAccount();

            myBank.calculateNewBalance();
        }
    }
}

银行账户class

package com.company;

import javax.swing.*;

public class BankAccount
{
    private int accountNumber;
    private String accountType;
    private double minSavings = 2500.00;
    private double minChecking = 1000.00;
    private double currentBalance;

    public BankAccount()
    {
        accountNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account number"));
        accountType = JOptionPane.showInputDialog("Please enter your account type");
        currentBalance = Double.parseDouble(JOptionPane.showInputDialog("Please enter your current balance."));
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void setAccountNumber(int accountNumber)
    {
        this.accountNumber = accountNumber;
    }

    public String getAccountType()
    {
        return accountType;
    }

    public void setAccountType(String accountType)
    {
        this.accountType = accountType;
    }

    public double getMinSavings()
    {
        return minSavings;
    }

    public void setMinSavings(double minSavings)
    {
        this.minSavings = minSavings;
    }

    public double getMinChecking()
    {
        return minChecking;
    }

    public void setMinChecking(double minChecking)
    {
        this.minChecking = minChecking;
    }

    public double getCurrentBalance()
    {
        return currentBalance;
    }

    public void setCurrentBalance(double currentBalance)
    {
        this.currentBalance = currentBalance;
    }

    public void calculateNewBalance()
    {
        if (accountType.equals("S") || accountType.equals("s"))
        {
            accountType = "Savings";
            calculateSavingsBalance();
        } else if (accountType.equals("C") || accountType.equals("c"))
        {
            accountType = "Checking";
            calculateCheckingBalance();
        }
    }

    private void calculateSavingsBalance()
    {
        if (currentBalance >= minSavings)
        {
            double newBalance = currentBalance + (currentBalance * .04 / 12);

            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(currentBalance < minSavings)
        {
            isServiceCharge();
        }
    }

    private void calculateCheckingBalance()
    {
        if (currentBalance > 6000)
        {
            double newBalance = currentBalance + (currentBalance * .03 / 12);
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if (currentBalance >= minChecking)
        {
            double newBalance = currentBalance + (currentBalance * .05 / 12);
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(currentBalance < minChecking)
        {
            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);
        }
    }
}

您的代码中实际发生了什么,

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";

for c or C accountType = "Checking";

但有趣的是 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);
    }

}

这是一个小问题

如果您看到此函数,您正在将帐户类型更改为 '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"))

它将如您所愿地工作