0

Odd rounding has me confused in the second Java practice problem

I'm not sure if I missed something in the modules about rounding, but this has really got me stumped. In the challenge for module 2, I set up a simple loop for the loan calculator, but my result is always off by 1. Sometimes it's above, and apparently sometimes it isn't. What I *think* is happening is that they're using rounded numbers, because I can see in their example problem that one of the numbers is rounded. But I don't think I was ever shown how to do that? It's not mentioned anywhere that I should round. I tried it a couple ways with the help of a friend, but those didn't fix all of the test cases, only some (I dont have pro so I cant tell what was wrong) Can anyone nudge me in the right direction here? Is there something I missed that I could review? I'll see if I can post my current code in the comments or something

17th Mar 2021, 10:43 AM
Zichqec
Zichqec - avatar
5 Respostas
+ 1
If you observe the sample, then it says need to round up the amount. so Use int paid = Math.ceil(owed/10.0);
17th Mar 2021, 11:01 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 I copied all of the text from the challenge's page into a reply here, there is absolutely nothing about needing to round up, except for the month 6 example where they don't point it out and don't explain how they did it. As a note, I'm on mobile, but I think I checked on desktop too and it was the same. At this point in the course, they've only explained basic variable types, basic operator types, and flow control. I definitely haven't seen Math.ceil() before, so I was pretty lost. Thank you very much, I'll give that a try!
17th Mar 2021, 2:33 PM
Zichqec
Zichqec - avatar
+ 1
Uh oh, now it's complaining about lossy conversion between doubles and ints, but they also have not shown me how to convert between types. I wonder if this challenge ended up out of order somehow? I shouldn't have to be googling so much to complete these lol Hmm...
17th Mar 2021, 2:39 PM
Zichqec
Zichqec - avatar
0
//I know this is kind of janky, I just want to understand what the rounding issue is import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); //your code goes here int owed = amount; for (int month = 0; month < 6; month++) { int paid = owed / 10; owed -= paid; } System.out.println(owed); } }
17th Mar 2021, 10:45 AM
Zichqec
Zichqec - avatar
0
"You take a loan from a friend and need to calculate how much you will owe him after 6 months. You are going to pay him back 10% of the remaining loan amount each month. Create a program that takes the loan amount as input, calculates and outputs the remaining amount after 6 months. Sample Input: 20000 Sample Output: 10628 Here is the monthly payment schedule: Month 1 Payment: 10% of 20000 = 2000 Remaining amount: 18000 Month 2 Payment: 10% of 18000 = 1800 Remaining amount: 16200 Month 3: Payment: 10% of 16200 = 1620 Remaining amount: 14580 Month 4: Payment: 10% of 14580 = 1458 Remaining amount: 13122 Month 5: Payment: 10% of 13122 = 1313 Remaining amount: 11809 Month 6: Payment: 10% of 11809 = 1181 Remaining amount: 10628 Use a loop to calculate the payment and remaining amounts for each month. Also, use integers for amounts."
17th Mar 2021, 2:28 PM
Zichqec
Zichqec - avatar