为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

冉冉说 2021-10-13 13:58:25
我几乎完成了这个银行账户程序。我有一个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();        }    }}
查看完整描述

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

它会按您的预期工作


查看完整回答
反对 回复 2021-10-13
?
慕姐8265434

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

    }


}


查看完整回答
反对 回复 2021-10-13
  • 2 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信