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

时间:2018-09-26 16:46:43

标签: java if-statement methods method-call

我几乎完成了这个银行帐户计划。我打电话给b方法或a方法。如果import itertools, collections pairs = itertools.product(a, b) results = collections.defaultdict(list) for sub_one, sub_two in pairs: comparison = good_stuff(sub_one) == good_stuff(sub_two) results[tuple(sub_one)].append(comparison) results[tuple(sub_two)].append(comparison) for sublist, comparisons in results.items(): if any(comparisons): continue print(sublist) # or from pprint import pprint results = [sublist for sublist, comparisons in results.items() if not any(comparisons)] pprint(results) 小于所需的calculateSavings()calculateCheckings(),则我有一条currentBalance语句来调用minSavings方法以扣除费用。该程序没有给我错误,但是如果用户输入的minChecking小于elseisServiceCharge()所需的数量,则currentBalance不会显示输出。输入仍然有效,但是没有弹出输出。其他一切都可以正常工作,例如增加利息,但不能从服务费中扣除费用。我怎样才能解决这个问题?

主班

minChecking

BankAccount类

minSavings

2 个答案:

答案 0 :(得分:0)

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

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

c或C帐户类型=“正在检查”;

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

}

答案 1 :(得分:0)

这是一个小问题

如果看到此功能,您正在将帐户类型更改为“储蓄”或“检查中”

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

它将按预期工作