-
Simple Method Question
I have the following code, and I want to make the return statement loop but i'm not sure exactly what i'm doing wrong or if it is even possible.
Code:
import javax.swing.JOptionPane;
public class investment {
public static void main(String[] args) {
String dataString = JOptionPane.showInputDialog(
"Enter the investment amount: ");
double amount = Integer.parseInt(dataString);
String dataString1 = JOptionPane.showInputDialog(
"Enter the interest rate: ");
double interest = Integer.parseInt(dataString1);
String dataString2 = JOptionPane.showInputDialog(
"Enter the amount of years: ");
int years = Integer.parseInt(dataString2);
System.out.println("Years" + " " + "Yearly Amount");
System.out.println("______" + " " + "____________");
System.out.println(investment(amount,interest,years));
}
public static double investment(double investmentAmount, double monthlyInterestRate, int years){
double realrate = monthlyInterestRate / 100;
double monthlyPayment = investmentAmount * realrate / (1 - 1 / Math.pow(1 + realrate, years * 12));
total = monthlyPayment + investmentAmount;
for (i = 1; i <= years; i++)
double realtotal = total + monthlyPayment
System.out.println(i);
return realtotal;
}
}
I want the output to look something like:
Year 1 - 1093
Year 2 - 1186
Year 3 - 1279
and so forth looping through the amount of years
-
Once a return statement is executed the method is exited and the code returns to where the method was called.
I would suggest using an array to return your values.
-
Yes, return statement can't loop several times. What you can do is, store your results in an array and return it. It's easy.