我的循环有什么问题

时间:2014-09-26 17:09:15

标签: java

我是一名Java初学者,试图让这个代码列表保存8年并节省下来的利息。我正在尝试使用for循环。它计算利息,但只列出1年。非常感谢解决这个问题的任何帮助。

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;  
   }

}

3 个答案:

答案 0 :(得分:2)

您的return循环中有一个for语句。因此,第一次迭代返回,立即退出for循环。

return循环之后,您的for语句已经位于正确的位置,所以您只需删除return语句中的for语句}循环。

答案 1 :(得分:0)

将你的循环改为:

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));
} 

删除退货。一旦返回,它就会停止执行该函数并返回给调用者。

答案 2 :(得分:0)

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

} 

问题是循环内的return。它执行一次迭代,当它到达return语句时,它退出循环。我想你想把它移到循环之外。

评论后更新:试试这个

 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;
}

请注意,这会以您希望的方式显示内容,但我认为您的计算在某处出错。