gpt4 book ai didi

Java Interest Calculator 无法编译,不知道为什么

转载 作者:行者123 更新时间:2023-11-29 08:02:16 25 4
gpt4 key购买 nike

我正在用 Java 编写一个利息计算器。该程序提示用户输入,并使用该输入计算特定银行帐户(支票、储蓄或 CD)的利息。

这就是我的程序的要点,而且非常简单。但是现在我对 return 语句在 createAccount 方法中不起作用的确切原因感到困惑。任何帮助将不胜感激。

银行家.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();
}
}

账户.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();
}
}

最佳答案

首先,createAccount 的返回类型是Account,您从它返回一个String。它会在那里失败。

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

但同样,很难理解为什么要从 createAccount 方法返回一个 string。事实上,您根本没有使用该方法创建任何帐户。请重命名您的方法以反射(reflect)其确切用途。

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

if (input == "Checking")

应该是:-

if (input.equals("Checking"))

关于Java Interest Calculator 无法编译,不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13653939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com