Java 我的循环有什么问题
我是一名Java初学者,试图让这段代码列出8年来节省的金额和利息。我正在尝试使用for循环。它计算利息,但只列出1年的利息。如果您能帮我解决这个问题,我将不胜感激Java 我的循环有什么问题,java,Java,我是一名Java初学者,试图让这段代码列出8年来节省的金额和利息。我正在尝试使用for循环。它计算利息,但只列出1年的利息。如果您能帮我解决这个问题,我将不胜感激 import javax.swing.JOptionPane; import java.text.*; public class RetirementGoal3 { private static DecimalFormat percentFormat = new DecimalFormat("###.##%"); pr
import javax.swing.JOptionPane;
import java.text.*;
public class RetirementGoal3
{
private static DecimalFormat percentFormat = new DecimalFormat("###.##%");
private static DecimalFormat moneyFormat = new DecimalFormat("$###,###,###.00");
private static double interest;
private static double saveAmount;
private static double total;
private static int Max_Year = 8;
private static int year;
private static double totalSave;
private static double totalInterest;
public static void main(String[] args)
{
calculateR();
}
public static double calculateR()
{
String result = JOptionPane.showInputDialog(null, "Enter how much money you can save annually.");
if(result != null)
saveAmount = Double.parseDouble(result);
String result1 = JOptionPane.showInputDialog(null, "Enter your interest rate(As a decimal)");
if(result1 != null)
interest = Double.parseDouble(result1);
totalSave = saveAmount;
for(year = 1; year <= Max_Year; ++ year)
{
totalInterest = saveAmount + saveAmount * interest;
JOptionPane.showMessageDialog(null, " If you save " + moneyFormat.format(totalSave) + " each year for " + Max_Year + " years" + "\n With " + percentFormat.format(interest) + " Interest" + " Without Interest" + "\n\nAfter year " + year + " " + moneyFormat.format(totalSave) + " " + moneyFormat.format(totalInterest));
return total;
}
return total;
}
}
import javax.swing.JOptionPane;
导入java.text.*;
公共类退休目标3
{
私有静态DecimalFormat percentFormat=新的DecimalFormat(“###.##%”);
private static DecimalFormat moneyFormat=新的DecimalFormat($),$,$,$,$,$;
私人静态双重利益;
私人静态双重储蓄额;
私人静态双总;
私人静态整数最大年=8;
私人静态int年;
私有静态双重存储;
私人静态双重利益;
公共静态void main(字符串[]args)
{
计算器();
}
公共静态双计算器()
{
String result=JOptionPane.showInputDialog(null,“输入您每年可以节省多少钱”);
如果(结果!=null)
saveAmount=Double.parseDouble(结果);
String result1=JOptionPane.showInputDialog(null,“输入您的利率(作为小数点)”);
如果(result1!=null)
利息=Double.parseDouble(结果1);
totalSave=储蓄金额;
对于(year=1;year在for
循环中有一个return
语句。因此,第一次迭代返回,立即退出for
循环
在for
循环之后,您的return
语句已经在正确的位置,因此您所要做的就是删除for
循环中的return
语句。将循环更改为:
for(year = 1; year <= Max_Year; ++ year)
{
totalInterest = saveAmount + saveAmount * interest;
JOptionPane.showMessageDialog(null, " If you save " + moneyFormat.format(totalSave) + " each year for " + Max_Year + " years" + "\n With " + percentFormat.format(interest) + " Interest" + " Without Interest" + "\n\nAfter year " + year + " " + moneyFormat.format(totalSave) + " " + moneyFormat.format(totalInterest));
}
for(年份=1;年份)
请注意,这将以您希望的方式显示内容,但我认为您的计算在某个地方出错。我想感谢所有人的循环问题,但它并没有在一个窗格中列出所有8年。它显示了多少年?请尝试更改此(year=1;year它为每年显示一个窗格,而不是在一个窗格中列出所有8个。例如,year 1窗格命中enter新窗格是year 2等,我想我需要将当前for循环嵌套在另一个for循环中。尝试过了,每次点击enter,它只会添加一行,直到达到8次。尝试获取一个包含8个ye的列表马上回答。嗯?你在哪里输入的?好的,你把我弄丢了。请更新这个问题,并提供一个预期的输出,而且要详细。因为现在我不确定你想要什么
for(year = 1; year <= Max_Year; ++ year)
{
totalInterest = saveAmount + saveAmount * interest;
JOptionPane.showMessageDialog(null, " If you save " + moneyFormat.format(totalSave) + " each year for " + Max_Year + " years" + "\n With " + percentFormat.format(interest) + " Interest" + " Without Interest" + "\n\nAfter year " + year + " " + moneyFormat.format(totalSave) + " " + moneyFormat.format(totalInterest));
return total; //<-- PROBLEM
}
String resultString = "";
for(year = 1; year <= Max_Year; year++)
{
totalInterest = saveAmount + saveAmount * interest;
resultString += " If you save " + moneyFormat.format(totalSave) +
" each year for " + Max_Year + " years" + "\n" +
" With " + percentFormat.format(interest) + " Interest" +
" Without Interest" + "\n\nAfter year " + year + " " +
moneyFormat.format(totalSave) +
" " + moneyFormat.format(totalInterest) + "\n";
}
JOptionPane.showMessageDialog(null, resultString);
return total;
}