0

Someone could help?

I tried the "loan calculator" in part 2 of Java. I dont know ANYTHING after it , so please if possible give me a solution possible with the knowledge I got... I tried a for loop with for(int month=1; month<=6; month++){ amount = amount - amount/10; if (month==6) System.out.println(amount);} to calculate the money I have to pay after 6 months. Please help!

17th Dec 2020, 11:43 PM
Kevin S
Kevin S - avatar
8 Réponses
+ 3
Here's both options with the Math.ceil() option commented out. double j = 0.0; int x = 0; for(int i = 0; i < 6; i++) { //amount -= (int)Math.ceil(amount * 0.1); j = amount / 10.0; x = amount / 10; if (j > x) { amount -= x + 1; } else { amount -= x; } } System.out.println(amount);
18th Dec 2020, 12:54 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Look at months 5 and 6 in the example. You'll see there how you need to round up if there is a fractional amount. Given what has been taught in the course so far, you can use a couple of variables, 1 a double and another an int to both perform the interest calculation. If the double is greater than the int, add 1 to the int and subtract it from the amount otherwise just subtract the int. OR You can just wrap what you have in Math.ceil() and cast it to an int, but I don't think this has been taught in the course yet.
18th Dec 2020, 12:21 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
amount / 10 will give you an int where when at least 1 of the numbers is a float type (10.0) it will give you a double. So, Yes, exactly! Lol
18th Dec 2020, 1:11 AM
ChaoticDawg
ChaoticDawg - avatar
0
I would go better if you initialize it for( in month= 1; month<= 6;month++)
17th Dec 2020, 11:49 PM
Daniel Briceño
Daniel Briceño - avatar
0
then it will run six times and not just one
17th Dec 2020, 11:50 PM
Daniel Briceño
Daniel Briceño - avatar
0
Sorry you are right , I did month=1 , just wrote it wrong here
17th Dec 2020, 11:50 PM
Kevin S
Kevin S - avatar
0
Could you give me an example code? I tried the first solution you told me but it doesnt work :/
18th Dec 2020, 12:46 AM
Kevin S
Kevin S - avatar
0
Thx I found my mistake! I did it nearly in the same way but I did j = amount /10; Does it handle amount as an int when i do /10 or whats the mistake here? :o
18th Dec 2020, 1:03 AM
Kevin S
Kevin S - avatar