+ 1

Java Loan Calculator

Hello guys, I posted this question earlier, but it's not showing up, even in my questions sections, so maybe somehow it didn't get posted, so I will write it again" I want to complete the Loan calculator code project, but I am running into an error that I don't know how to solve. I don't know how to solve it because I thought what I wrote is an operation that Java can execute under these circumstances, but it seems like it cannot which is why I am stuck on this problem. The error is: /usercode/Program.java:18: error: not a statement amount - .9*amount; ^ 1 error Next I will write what the problem says and how I tried to solve it so you can see what I did. Problem: You take a loan from a friend and need to calculate how much you will owe him after 3 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 3 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 Use a loop to calculate the payment and remaining amounts for each month. Also, use integers for amounts. My code: 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 while (x >= amount*.729) { System.out.println(amount); if (amount == .9*amount) { continue; } if (amount == .81*amount) { continue; } amount - .9*amount; } }

29th Aug 2022, 4:27 AM
Iulia
8 odpowiedzi
+ 2
Have a search in the forum (wouldn't have been possible the last few days, I don't think). I believe there's a problem with your loop and part of the logic. https://www.sololearn.com/discuss/2995588/?ref=app
29th Aug 2022, 7:48 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
AJ means use the formula amount = amount - amount * 10 / 100; inside a while loop. Maybe like this: int loopcounter = 3; while(loopcounter != 0) { amount = amount - amount * 10 / 100; loopcounter--; }
29th Aug 2022, 11:58 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Iulia You are not assigning (amount - 0.9 * amount) to a variable And also 0.9 not .9 Also why amount * .729 Ans where is .9 or .81 in task? formula is amount = amount - amount * 10 / 100; simple just use this inside loop
29th Aug 2022, 9:54 AM
A͢J
A͢J - avatar
+ 1
Iulia Check your question, you have mentioned "error: not a statement" If you don't assign a statement to a variable or you don't print statement, you will get this error so there should be amount = amount - 0.9 * amount;
29th Aug 2022, 12:30 PM
A͢J
A͢J - avatar
+ 1
No .9 Yes 0.9
29th Aug 2022, 9:32 PM
Eliakim Ramos
Eliakim Ramos - avatar
0
Hello A͢J, what do you mean when you say that I am not assigning (amount - 0.9*amount) to a variable? I meant 1) to put System.out.println(amount - 0.9*amount) in the last line instead of writing it without the print statement, which I have done now, and 2) I also meant to put "amount" instead of x at the start of the while loop, which I fixed, but now the problem is that the "Execution timed out", but I don't know why it timed out. Also, the reason I didn't print the amount*0.9 and amount*0.81 because the problem asked to print how much the man would owe after 3 months. So the new loop says: while (amount >= amount*0.729) { System.out.println(amount); if (amount == 0.9*amount) { continue; } if (amount == 0.81*amount) { continue; } System.out.println(amount - 0.9*amount); } Also, what do you mean by just use this inside loop? Thanks, Iulia
29th Aug 2022, 11:34 AM
Iulia
0
amount - 0.9*amount ; is no effect statement. You need to store result to make it valid and use later. amount = (int)(amount - 0.9*amount); //cast to int And your continue statements may cause infinite loop. And i guess, you if condition never comes true because of any floating value. Amount is integer value and 0.9*amount will have precision value so won't equal until precision value is zero. Don't print result in loop. Task asked to print only final result. You need find 10% of amount, subtract it from amount. Repeat this only 3 times in loop. Finally after loop print result amount.
29th Aug 2022, 12:04 PM
Jayakrishna 🇮🇳
0
Hold on guys, I need to get back to this problem.
29th Aug 2022, 4:20 PM
Iulia