[英]Java Interest Calculator won't compile, not sure why
I am writing an interest calculator in Java. 我正在用Java编写兴趣计算器。 The program prompts the user for input, and using that input calculates interest on a certain bank account (checking, savings or CD).
该程序提示用户输入,并使用该输入计算特定银行帐户(支票,储蓄或CD)的利息。
That is the gist of my program and it is pretty simple. 那是我程序的要旨,这很简单。 But right now I am stuck with exactly why the return statement is not working in the createAccount method.
但是现在,我完全坚持为什么return语句在createAccount方法中不起作用的原因。 Any help would be appreciated.
任何帮助,将不胜感激。
Banker.java: 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 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();
}
}
First of all, the return type of createAccount
is Account
and you are returning a String
from it. 首先,
createAccount
的返回类型是Account
并且您要从中返回一个String
。 It will fail there. 它将在那里失败。
So, change your return type to String
. 因此,将您的返回类型更改为
String
。 And also to ensure that your method always returns a value
. 并且还要确保您的方法始终返回一个
value
。 You should return a value from every path your code may follow. 您应该从代码可能遵循的每个路径中返回一个值。 Alternatively, you can add a
return null;
或者,您可以添加
return null;
at the end of the method (But you should also consider the previous statement). 在方法的末尾(但您还应该考虑前面的语句)。
But again, it's hard to understand that why you are returning a string
, from createAccount
method. 但是同样,很难理解为什么要从
createAccount
方法返回一个string
。 And in fact you are not creating any account in that method at all. 实际上,您根本不会在该方法中创建任何帐户。 Please rename your method so as to reflect its exact purpose.
请重命名您的方法,以反映其确切目的。
Secondly, you are comparing your strings
using ==
operator, that will give you problems, once you come out of Compiler errors. 其次,您正在使用
==
运算符比较strings
,一旦遇到编译器错误,这将给您带来问题。 You should use equals
method to compare string: - 您应该使用
equals
方法比较字符串:-
if (input == "Checking")
should be: - 应该: -
if (input.equals("Checking"))
另外, if-else
没有在if-else
的分支之一中创建Account,则必须添加return
子句。
Well First, follow Rohit's suggestions. 首先,请遵循Rohit的建议。
The other major problem is that not all your code paths return a value at all. 另一个主要问题是,并非所有代码路径都返回一个值。 (What happens if the input is "checking" ?)
(如果输入是“ checking”,会发生什么?)
Second, path that IS returning a value is placed AFTER the system Exit: 其次,将要返回值的路径放置在系统退出之后:
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
}
}
}
Are you using an IDE? 您正在使用IDE吗? or just using a notepad?
还是只使用记事本? Are you paying attention to the compiler warnings?
您是否注意编译器警告? Always take care of warnings, they're likely latent runtime errors.
始终注意警告,它们很可能是潜在的运行时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.