Hello all. This is my first time posting here. I am hoping to get some advice on a homework program I am doing. I am completely lost on this particular program. I am finding nested loops to be very confusing. I tend to "get lost" in them. Here is what I am doing:
I am supposed to write a program that finds the monthly payment for 30 years, for principals from $100,000 through $200,000(with increments of $20,000) and interest rates of 6% through 10% (with increments of 0.5%). The output of the file must be in a table that looks something like this:
I have tried adding his loop as the first loop, the second loop and the last loop to no avail. I've tried putting the last System.out.println(monthlyPayment) within the first for loop curly bracket, the second and the last. I am completely at a loss. Here is the latest I tried:
I would appreciate it if someone could steer me the right direction.
The first loop is for the rows (line 17); it goes from 100,000 to 200,000 with increments of 20,000.
The second loop is for the columns in each row (line 21), it goes from .06 to .101 in increments of .005.
The printout in line 29 is in the wrong place. It should be inside the inner loop, but now it is not. Put this line between line 27 and 28.
Furthermore, you're printing newlines in some places in such a way that you don't get a neat table output as you intend. You only want to print a newline at the end of a row - not after every number.
In line 18, you don't want to print newlines. Remove the "\n", and change println() to print(). Maybe you want to print some spaces after the number L, so change this to something like:
Also, in line 29 (that should be moved to between 27 and 28) you also don't want to print a newline. Change this to something like:
Then at the end of each row you want to print a newline. Put this in line 29 (outside the inner loop, but inside the outer loop):
That takes care of the formatting, but then there's still something wrong with the calculations (I get a table full of zeroes when I run it). Check carefully, by following the program line by line in your head, keeping track of the values of the variables, what it's actually calculating, and if this is what you expect or not.
About this code (lines 12-14):
Why did you write it that way? You could just write it out as one string: