【发布时间】:2016-02-24 02:57:56
【问题描述】:
显然,我并不是在寻找所有的答案,但我会非常感谢一些方向。
所以我有这个程序,提示用户输入他们的年利率和储蓄金额。程序计算6个月的复合值。
我在将新方法调用到主要方法中以执行计算时遇到问题。主要方法是询问用户他们的年利率和储蓄金额。它调用新方法,如果都是正数,它就会执行程序。如果有任何负数,则会出现错误。
我以为我在最后一行调用了该方法,但显然我错了。我试过谷歌搜索,但无法理解这个调用方面。任何信息,将不胜感激。我是新手,所以我还在做这个编程的事情!不知道为什么最后一个大括号不在代码中。代码如下:
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class FifthAssignment {
static double savingAmount = 0.0; // the 2 given static doubles
static double annualInterestRate = 0.0;
int sixthMonth;
public static double compoundValueMethod(double savingAmount, double annualInterestRate, int sixthMonth) {
return sixthMonth; // sixMonth is the compound value
}
{
DecimalFormat formatter = new DecimalFormat(".00");
double monthlyInterestRate = annualInterestRate / 12;
if (savingAmount < 0)
if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
System.exit(0);
}
if (savingAmount < 0) {
JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
}
else if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
}
else {
for (int i = 1; i <= 6; i++) {
sixthMonth = (int) ((savingAmount + sixthMonth) * (1 + monthlyInterestRate));
}
}
}
public static void main(String[] args) {
DecimalFormat formatter = new DecimalFormat(".00"); // bring in the
// decimal
// formatting for
// program
String SA = JOptionPane.showInputDialog("What is your savings amount? "); // Window
// pops
// up
// asking
// for
// your
// savings
// amount.
// As
// a
// string?
savingAmount = Double.parseDouble(SA); // Returns a double given to
// value in string above
String AIR = JOptionPane.showInputDialog("What is the annual interest rate? "); // Window
// pops
// up
// asking
// for
// annual
// interest
// rate.
// String
// as
// above.
annualInterestRate = Double.parseDouble(AIR); // Returns the same as
// savings amount but
// for annual interest
// rate
{
JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is "
+ compoundValueMethod(savingAmount, annualInterestRate, 6));
}
}
}
【问题讨论】:
标签: java methods static-methods