2012-11-30 50 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

相关问题