【问题标题】:If else statement not calling isServiceCharge() method?if else 语句不调用 isServiceCharge() 方法?
【发布时间】:2018-09-26 16:46:42
【问题描述】:

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

主类

package com.company;

import javax.swing.*;

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

            myBank.calculateNewBalance();
        }
    }
}

银行账户类

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);
        }
    }
}

【问题讨论】:

  • 你应该学习如何使用调试器...

标签: java if-statement methods method-call


【解决方案1】:

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

public void calculateNewBalance()
{
    if (accountType.equals("S") || accountType.equals("s"))
    {
        accountType = "Savings";
        calculateSavingsBalance();

    } else if (accountType.equals("C") || accountType.equals("c"))
    {
        accountType = "Checking";
        calculateCheckingBalance();
    }


}

对于 s 或 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);
    }

}

【讨论】:

    【解决方案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();
        }
    
    
    }
    

    然而,您在

    中将 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"))
    

    它会按照你的预期工作

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 2015-03-13
      • 2019-04-09
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 2015-07-06
      • 2012-08-05
      相关资源
      最近更新 更多