I'm just starting out with Java and I'm stuck with this problem, for which I can only use For loops and the Math class. The problem is that I can not increase the rate. This is what should appear on the screen:
5 years 250.0(1.0%) 375.0(1.5%) 500.0(2.0%) 625.0(2.5%)
6 years 300.0(1.0%) 450.0(1.5%) 650.0(2.0%) 750.0(2.5%)
7 years 350.0(1.0% ) 525.0(1.5%) 700.0(2.0%) 875.0(2.5%)
8 years 400.0(1.0%) 600.0(1.5%) 800.0(2.0%) 1000.0(2.5%)
and this what I get:
5 years 250.0(1.0%) 250.0(1.5%) 250.0(2.0%) 250.0(2.5%)
6 years 250.0(1.0%) 250.0(1.5%) 250.0(2.0%) 250.0(2.5%)
7 years 250.0(1.0% ) 250.0(1.5%) 250.0(2.0%) 250.0(2.5%)
8 years 250.0(1.0%) 250.0(1.5%) 250.0(2.0%) 250.0(2.5%).
This is the code I have so far:
double capitalInicial = 5000.0;
double tasa = 1.0;
int anios = 5;
double intereses = capitalInicial * tasa / 100 * anios;
for(int i = 5; i <= 8; i++) {
System.out.println(i + " años \t" + intereses + "(1.0%) \t" + intereses + "(1.5%) \t" + intereses + "(2.0%) \t" + intereses + "(2.5%) \t");
I appreciate the help.
Problem:
You would have to move a
double intereses = capitalInicial * tasa / 100 * anios;
inside your loopfor
and replace aanios
with the for (i
) iterator, since itintereses
was only being calculated once, which is why the same result was repeated every time.Solution:
Now, for you to have the same results as the exercise, you would have to multiply
intereses
by the percentages requested (1.0%, 1.5%, 2.0%, 2.5%) . Let's see:Output:
It is because
intereses
it is calculated only once. For it to change each year it must be inside thefor
, and the counter can be usedi
as the value of the year:Thank you very much for the answers, indeed the problem was calculating interest outside of the For and also not multiplying the interest. Thanks again and I hope to continue learning.