Java兴趣计算器不会编译,不知道为什么

时间:2012-11-30 22:12:06

标签: java if-statement syntax-error

我正在用Java编写兴趣计算器。该程序提示用户输入,并使用该输入计算某个银行帐户的利息(检查,储蓄或CD)。

这是我的程序的要点,它非常简单。但是现在我很难理解为什么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();
}
}

3 个答案:

答案 0 :(得分:5)

首先,createAccount的返回类型为Account,您将从中返回String。它会在那里失败。

因此,请将您的返回类型更改为String。并确保您的方法始终返回value。您应该从代码可能遵循的每个路径返回一个值。或者,您可以在方法的末尾添加return null;(但您还应该考虑前面的语句)。

但同样,很难理解为什么要从string方法返回createAccount。事实上,你根本就没有在该方法中创建任何帐户。请重命名您的方法以反映其确切目的。

其次,您正在比较strings使用==运算符,这会在您出现编译器错误时给您带来问题。您应该使用equals方法来比较字符串: -

if (input == "Checking")

应该是: -

if (input.equals("Checking"))

答案 1 :(得分:0)

如果未在return的某个分支内创建帐户,则必须添加if-else子句。

答案 2 :(得分:0)

首先,请遵循Rohit的建议。

另一个主要问题是并非所有代码路径都返回一个值。 (如果输入是“检查”会发生什么?)

其次,IS返回值的路径放在系统退出后:

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吗?或只是使用记事本?你在关注编译器警告吗?始终处理警告,它们可能是潜在的运行时错误。