- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 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/
我知道 Magical Record 支持聚合操作,例如 sum:、max: 但是有没有办法进行一些简单的计算,例如: 总和:属性 * other_attributes 如果我们知道这些属性的值为 N
我有一个项目可以计算一些关于用户表现的“统计数据”,然后将其展示给他们。所有这些统计数据最终都来自一个记录用户与网站交互的大型“交互”表。目前,所有这些统计数据都是通过查看这些数据来计算的。我们广泛使
我正在试着用熊猫和NumPy来计算蟒蛇中的Connors RSI。我想用ConnorsRSI的默认值(3,2,100)来计算它。。Connors RSI的公式为:[RSI(Close,3)+RSI(S
我对某种 mean() 计算有疑问。我使用带有两个标识符“ID”和“year”的面板数据集(使用 plm pkg) 我想计算变量“y”的分组平均值,但省略了第一年的计算条目,然后仅填写用于计算它的年份
我不知道这是否是微不足道的或实际上很棘手:是否可以捕获 VBA 中的“计算工作表 (shift+f9)”和“计算工作簿”事件? 我想隐藏一些操作几千行的进程,只显示一些关键值。我正在计算分布,数千行,
我和#1895500有同样的问题, 但使用 PostgreSQL 而不是 MySQL。 如何定义具有计算字段的 View ,例如: (mytable.col1 * 2) AS times_two .
如何定义具有两个计算字段的 View ,例如... ('TableName'.'BlueSquares' + 'TableName'.'RedSquares') AS TotalSquares, (
CALCULATE(m, x=red) 和 CALCULATE(m, KEEPFILTERS(x=red)) 之间有什么区别 显然它们不一样。我找到了文档和解释,但我仍然不明白。 https://le
我正在尝试从命令提示符运行我的 Java 类文件,当我尝试这样做时,我收到此错误 C:\Users\New User\workspace\myproject\bin\apackage>java cal
我正在尝试根据用户的输入显示文本。例如输入单词 APPLE 应该让它显示 BANANA。 这段代码工作正常: :Input X :If X=APPLE :Disp "BANANA" 但我不确定如何以此
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我们正在尝试实现自己的自定义购物篮计算规则集并注册新的结果 View 来获取购物篮计算结果,但我们无法找到一些如何注册新结果 View 类的信息? 我们使用这里的示例:https://support.
数字变量是否遵循 TI 计算器上的记录标准? 我真的很惊讶地注意到我的 TI 83 Premium CE 测试实际上返回了 true(即 1): 0.1 -> X 0.1 -> Y 0.01 -> Z
大约两天前,我收到了我的 TI-82 STATS 可编程计算器(实际上更像是一个 TI-83) - 并想用内置的 TI-BASIC 语言编写一个贪吃蛇游戏。 虽然我不得不找出:TI-BASIC 是 极
作为家庭作业,我们有一个基本的计算器,它只能进行+运算,我们必须实现更多的功能。我们必须实现括号运算符、符号运算符和最小最大函数。最后的任务之一是扩展最小/最大函数以计算具有两个以上参数的最小/最大,
如何从 Excel 的单元格中选择一列,然后仅计算该列?我只知道 SHIFT + F9 可以计算整个工作表,F9 可以计算整个工作簿。 谢谢你们;) 最佳答案 我认为仅使用标准 Excel 无法做到这
我已经为计算器编写了代码,但它还不能 100% 可靠地工作。每次我进行计算时,例如:“1+1=2”,并且我想要进行另一次计算,我必须关闭小程序并重新启动它。我怎样才能让它回到开始的地方。 这是代码:
意图:该程序要求用户提供其银行帐户中当前的金额、年利率和年数。输出是金额的开始和结束,显示用户指定年份的累计利息。 问题:我正在尝试找到一种正确添加利息的方法,截至目前,在指定的年份里,我所做的就是乘
我怎么让第一次点击不接受操作返回“0” 这是我的功能 $(document).ready(function(){ $('button').on('click', function(){
题目地址:https://leetcode.com/problems/basic-calculator/description/ 题目描述 Implement a basic calculator
我是一名优秀的程序员,十分优秀!