What is wrong here? Can not proceed further [JAVA] | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

What is wrong here? Can not proceed further [JAVA]

Hello, colleagues! I completed Loam Calculator JAVA Challenge, but I can't proceed, as Code Analyzer showing message that I got a bug there. However 2 Test Cases passed, and output completely identical. There are 3 test cases that are hidden, may be something wrong there? Below is 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 int term = 3; for (int i = 1; i <= term; i++ ) { int payment = amount / 100 * 10; amount = amount - payment; } System.out.println(amount); } } Please help, I'm stuck :((

6th Oct 2021, 5:54 PM
Š˜Š³Š¾Ń€ŃŒ Š’Š¾Ń€Š¾Š½ŠøŠ½
Š˜Š³Š¾Ń€ŃŒ Š’Š¾Ń€Š¾Š½ŠøŠ½ - avatar
5 Respostas
+ 1
Try to use a double value in calculations. Try this. amount -=(amount/100.0 *10) In loop.. its equivalent to for (int i = 1; i <= term; i++ ) { double payment = amount / 100.0 * 10; amount = (int)(amount - payment); }
6th Oct 2021, 6:00 PM
Jayakrishna šŸ‡®šŸ‡³
+ 3
Thank you for the answer. But in task description asked to use Integers for amounts
6th Oct 2021, 6:06 PM
Š˜Š³Š¾Ń€ŃŒ Š’Š¾Ń€Š¾Š½ŠøŠ½
Š˜Š³Š¾Ń€ŃŒ Š’Š¾Ń€Š¾Š½ŠøŠ½ - avatar
+ 2
Š˜Š³Š¾Ń€ŃŒ Š’Š¾Ń€Š¾Š½ŠøŠ½ You can use integer also but you need to change your calculation logic like: for(int a = 1; a<=term; a++){ amount = amount - amount * 10 / 100; } System.out.println(amount) You are first dividing amount by 100 so issue is there. To understand problem see below problem: System.out.println (90 / 100 * 10); //0 System.out.println (90 * 10 / 100); //9 both will give different result
6th Oct 2021, 7:14 PM
AĶ¢J
AĶ¢J - avatar
0
Yes. Amount is of integer but in calculations for interest, if you use a double value then it won't miss praction part. Later you can convert back to int and display as I given above. Not changed the amount type but used a double type in calculations.. Check it once and reply if wrong.. Hope it helps...
6th Oct 2021, 6:09 PM
Jayakrishna šŸ‡®šŸ‡³
0
Question im 11 and how does a variable work
7th Oct 2021, 12:03 AM
David Stevens