亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果 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 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號