2012-11-30 51 views
1

我正在用Java編寫一個興趣計算器。該程序提示用戶輸入,並使用該輸入計算某個銀行賬戶的利息(支票,儲蓄或CD)。Java興趣計算器不會編譯,不知道爲什麼

這是我的程序的要點,它非常簡單。但是現在我被困在了爲什麼return語句不能在createAccount方法中工作。任何幫助,將不勝感激。

Banker.java:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Banker { 

// Array for type of bank account 
public static void createAndShowGUI() { 


    // Declare strings for period, balance, rate 
    String period; 
    String balance; 
    String rate; 
    String type; 
    String input; 

    // Prompt for account type 
    String[] accttype = {"Checking", "Savings", "CD"}; // Array of bank acct types 
    input = (String) JOptionPane.showInputDialog(null, "Choose account...", 
      "Choose bank account type", JOptionPane.QUESTION_MESSAGE, null, 
      accttype, // Array of acct types 
      accttype[0]); // First choice 


    // Prompt user for input 
    period = JOptionPane.showInputDialog(null, "Number of periods (length):"); 
    balance = JOptionPane.showInputDialog(null, "Beginning balance:"); 
    rate = JOptionPane.showInputDialog(null, "Interest rate (use decimal, example: .05 = 5%):"); 

    // Make Calculate button 
    JButton calculate = new JButton("Calculate"); 

    // Make 2 Labels 
    JLabel blabel = new JLabel("Period: " + period); 
    JLabel plabel = new JLabel("Balance: " + balance); 

    // Setup window with flow layout and exit on close 
    JFrame frame = new JFrame("Interest Savings Calculator Plus"); 
    frame.setLayout(new FlowLayout()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // Add combo box, calc button and labels 
    frame.add(calculate); 

    frame.add(plabel); 
    frame.add(blabel); 

    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 

} 

public static Account createAccount(String type, String checkno, String lengthm, String input) { 
    String message = "Would you like to open another account?"; 
    String title = "Are you sure?"; 
    if (input == "Checking") { 
     checkno = JOptionPane.showInputDialog(null, "First check number:"); 

     // display the JOptionPane showConfirmDialog 
     int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION); 
     if (reply == JOptionPane.YES_OPTION) { 
      System.exit(0); 

     } 
    } else if (input == "CD") { 
     lengthm = JOptionPane.showInputDialog(null, "Length until maturity:"); 

     // display the JOptionPane showConfirmDialog 
     int reply = JOptionPane.showConfirmDialog(null, message, title,  JOptionPane.YES_NO_OPTION); 
     if (reply == JOptionPane.YES_OPTION) { 
      System.exit(0); 
      return input; 
     } 
    } 
} 

public static void main(String[] args) { 
    createAndShowGUI(); 
} 
} 

Acccount.java

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Account implements ActionListener { 

JButton calculate; 
private int period; 
private int balance; 
private int fbalance; 
private int rate; 
private int monthlyFee; 
private String printstring; 

@Override 
public String toString() { 
    return String.format("Period: " + period + ", Balance: " + balance); 
} 

public int getPeriod() { 
    return period; 
} 

public void setPeriod(int period) { 
    this.period = period; 
} 

public int getBalance() { 
    return balance; 
} 

public void setBalance(int balance) { 
    this.balance = balance; 
} 

public int getRate() { 
    return rate; 
} 

public void setRate(int rate) { 
    this.rate = rate; 
} 

public int getFbalance() { 
    return fbalance; 
} 

public void setFbalance(int fbalance) { 
    this.fbalance = fbalance; 
} 

public String getPrintstring() { 
    return printstring; 
} 

public void setPrintString(String printstring) { 
    this.printstring = printstring; 
} 

public void calculate() { 
    for (int i = 0; i < period; i++) { 
     fbalance = balance + balance * rate - monthlyFee; 
    } 

} 

public void actionPerformed(ActionEvent e) { 
    calculate(); 
} 
} 
+0

「我一直堅持爲什麼return語句不能在createAccount方法中工作」你怎麼知道它不工作?它是否編譯?如果不是,那麼錯誤信息是什麼?如果是這樣,當你運行該程序時會發生什麼? –

+0

@ user1810496 ..我也看不到你在哪裏使用你的'createAccount'方法。 –

回答

5

首先的createAccount返回類型爲Account,你正在返回從它String。它會在那裏失敗。

因此,請將您的退貨類型更改爲String。並確保您的方法始終返回value。您應該從您的代碼可能遵循的每個路徑中返回一個值。或者,您可以在方法的末尾添加return null;(但您應該考慮前面的說明)。

但是,很難理解爲什麼你要返回string,從createAccount方法。事實上,你根本沒有在該方法中創建任何帳戶。請重新命名您的方法以反映其確切目的。

其次,您使用的==運營商,這將使你的問題,一旦你走出編譯器錯誤的比較,你strings。您應該使用equals方法來比較字符串: -

if (input == "Checking") 

應該是: -

if (input.equals("Checking")) 
+0

+1您需要確保始終返回一個值。 – mellamokb

+0

@mellamokb。謝謝。編輯帖子以反映該部分。 –

0

還必須添加return條款時,裏面的if-else的一個分支沒有創建帳戶。

0

那麼首先,按照Rohit的建議。

另一個主要問題是並不是所有的代碼路徑都返回一個值。 (如果輸入「檢查」,會發生什麼?)

二,也就是返回值被放置在系統退出後的路徑:

public static Account createAccount(String type, String checkno, String lengthm, String input) { 
    String message = "Would you like to open another account?"; 
    String title = "Are you sure?"; 
    if (input == "Checking") { 
     checkno = JOptionPane.showInputDialog(null, "First check number:"); 

     // display the JOptionPane showConfirmDialog 
     int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION); 
     if (reply == JOptionPane.YES_OPTION) { 
      System.exit(0); 

     } 
     // Return if input == checking not here 
    } else if (input == "CD") { 
     lengthm = JOptionPane.showInputDialog(null, "Length until maturity:"); 

     // display the JOptionPane showConfirmDialog 
     int reply = JOptionPane.showConfirmDialog(null, message, title,  JOptionPane.YES_NO_OPTION); 
     if (reply == JOptionPane.YES_OPTION) { 
      System.exit(0); 
      return input; // never gets here 
     } 
    } 
} 

是否使用一個IDE?或只使用記事本?你關注編譯器警告嗎?始終照顧警告,他們可能是潛在的運行時錯誤。

+0

謝謝Rohit我做了你的更正。這允許我的程序進行編譯。仍然有一些問題,但感謝那些提示用戶。我實際上注意到這是我忘記的一大部分:它不應該System.exit我實際上從一個例子複製粘貼,忘記編輯它。當用戶選擇YES返回到createAccount並重新循環時,我確實希望存儲該值。如果沒有將這些值傳遞給計算器?我**真的很感謝你們給予的任何幫助。我的程序的邏輯還不完善,所以如果你有任何想法,那麼他也會 – BakeMeMuffins92

+0

如果你想讓一個用戶能夠創建多個帳戶,你不應該在createAccount方法中處理該邏輯;您需要在調用createAccount方法的方法中實現它。基本思想是,在一個無限循環內,你詢問用戶他們是否想創建一個帳戶,如果是,請調用createAccount,如果不是,則跳出循環(和System.exit(0)或任何你想做的事) – BrDaHa

相關問題