+ 1

Loan calculator Java.

Hi, I've been trying to complete the lean calculator on java. I'll get cases 1,2, and 5 right but 3 and 4 wrong. I'm not sure why or how. A friend said that it's a rounding issue however I can't see cases 3 and 4 because of the paywall. Any help on why this is wrong would be great. The answer has to be an integer and it has to calculate for out to 6 months. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = scanner.nextInt(); for(int x = 0; x < 6; x++){ //this is where u set the counter for the months up amount*=.90;// this is where the loan is calculated } System.out.println ((int)amount); //this is converting amount into an integer. } }

25th Feb 2021, 4:20 PM
Alyson J
Alyson J - avatar
5 Respostas
+ 2
littlems AJ Do this amount = amount * 90 / 100;
25th Feb 2021, 4:41 PM
A͢J
A͢J - avatar
+ 1
@IAmAJ! Same issue sadly. Thank you tho!
25th Feb 2021, 4:51 PM
Alyson J
Alyson J - avatar
+ 1
littlems AJ Problem is in taking input. You declared amount as double but used nextInt() to get value so change amount as int type. Try this import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); for(int i = 0; i < 6; i++) { amount = amount * 90 / 100; } System.out.print(amount); } }
25th Feb 2021, 4:59 PM
A͢J
A͢J - avatar
+ 1
@IAmAJ thanks that worked! I was using () around the 90/100 before which is why it wasn't working when I last tried it. Thank you!
27th Feb 2021, 7:17 PM
Alyson J
Alyson J - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int loan; //your code goes here for(int i=1;i<=3;i++) { loan =amount/10; amount-=loan; } System.out.println(amount); } }
14th Jun 2021, 9:21 AM
Dharmi Sri
Dharmi Sri - avatar