Loan calculator Java. | Sololearn: Learn to code for FREE!

+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. } }

2/25/2021 4:20:33 PM

Alyson J

5 Answers

New Answer

+1

littlems AJ Do this amount = amount * 90 / 100;

+1

@IAmAJ! Same issue sadly. Thank you tho!

+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); } }

+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!

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); } }