简体   繁体   中英

What is wrong with my loop

I'm a beginner Java student trying to make this code list the amount saved and interest for 8 years. I'm trying to use a for loop for it. It calculates the interest but only lists it for 1 year. Would appreciate any help solving this problem.

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

}

You have a return statement inside your for loop. So, the first iteration returns, exiting the for loop right away.

You have a return statement already in the correct location, after the for loop, so all you have to do is remove the return statement that is inside the for loop.

Change you loop to :

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

Remove the return. As soon as you do a return it stops execution of that function and return to the callers.

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

} 

The problem is the return inside the loop. It executes one iteration and when it gets to the return statement it exits the loop. I guess you want to move it outside the loop.

Update after your comment: Try this

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

Note that this will display things the way you want to but i think your calculations go wrong somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM