Reputation: 3
In my code I am having the user type in 3 things, and for the third input I ask for the number of years. However in my for
loop I can't use the variable that I ask the user to input. For example if the user were to input "3", my code would do 15 years (which is the max).
double yearInvest;
double interRate;
double numOfYears = 3;
double amountBeforeTotal;
double amountTotal;
double years;
System.out.println("Compound Interest \n");
System.out.println("This program will print out a title table that will "
+ " display the amount of a yearly investment over a period of "
+ " up to 15 years. \n");
Scanner input = new Scanner(System.in);
System.out.println("Enter the yearly investment:");
yearInvest = input.nextDouble();
System.out.println("Enter the interest rate (%):");
interRate = input.nextDouble();
System.out.println("Enter the number of years:");
numOfYears = input.nextDouble();
for (years = 1; 15 >= years; years++) {
System.out.format("%-4s %22s %12s %8s \n ", "Year", "Amount in Account",
"Interest", "Total");
amountTotal = (yearInvest * (interRate / 100)) + yearInvest;
System.out.format("%-4.1f", years);
System.out.format("%18.2f", yearInvest);
System.out.format("%14.2f", interRate);
System.out.format("%15.2f", amountTotal);
}
P.S. I am still working on the code and it is not fully done. And I would also like some advice if possible.
Upvotes: 0
Views: 86
Reputation: 63
You can try this if you want to print only 15 years interest rates.
for (years = 1; numOfYears >= years; years++) {
System.out.format("%-4s %22s %12s %8s \n ", "Year", "Amount in Account",
"Interest", "Total");
amountTotal = (yearInvest * (interRate / 100)) + yearInvest;
System.out.format("%-4.1f", years);
System.out.format("%18.2f", yearInvest);
System.out.format("%14.2f", interRate);
System.out.format("%15.2f", amountTotal);
if(years == 15) {
break;
}
}
This will print the interest of 15 years if user enters any number greater than 15 or else will print interest rates of all years if user enters number < 15.
Upvotes: 0
Reputation: 2440
System.out.println("Enter the number of years:");
numOfYears = input.nextDouble();
for (years = 1; 15 >= years; years++)
Your code is getting a user inputted numOfyears
which for example would be 3, for your case. Your loop is from (1..15) no matter what, since your loop's 2nd parameter is: 15 >= years
.
What you are looking for is (1
..numOfYears
)
System.out.println("Enter the number of years:");
numOfYears = input.nextDouble();
for (years = 1; years <= numOfYears; years++)
//...more code
Upvotes: 1
Reputation: 33
There are a few things that I notice about your code that might not be exactly what you want.
Firstly you are storing all of your variables as doubles which are mainly used for storing floating point (i.e. numbers with a decimal place). In place of double it might be better to use int.
Next your for loop is always going to loop 15 times with years 1 to 15. If you want this to be only numOfYears times you should have a loop that compares to numOfYears
for (years = 1; years <= numOfYears; years++) {
//TODO
}
Lastly something that is quite important for coding and something that it is easy to ignore if teaching yourself is style.
for (years = 1; 15>=years; years++ ) {
System.out.format("%-4s %22s %12s %8s \n ", "Year", "Amount in Account", "Interest", "Total");
amountTotal = (yearInvest * (interRate / 100)) + yearInvest;
System.out.format("%-4.1f", years);
System.out.format("%18.2f", yearInvest);
System.out.format("%14.2f", interRate);
System.out.format("%15.2f", amountTotal);
}
This indentation gives a much clearer view of what is in your for loop and helps debugging and readablilty
Upvotes: 1