+ 1

Java lesson 20 can't pass test cases

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 for (int i = 0; i < 3; i++){ int payment = (amount / 100) * 10; amount = amount - payment; } System.out.println(amount); } } And it gives me the same output as the test case. But says that there is a bug. Can somebody help ?

20th Aug 2021, 6:33 AM
Sanon Selkov
Sanon Selkov - avatar
3 Respostas
+ 4
It's an integer precision problem. This works: for (int i = 0; i < 3; i++){ double payment = (amount / 100.0) * 10; amount = amount - (int) payment; } System.out.println(amount); And this solution as said Jay Matthews for (int i = 0; i < 3; i++){ int payment = amount/10; amount = amount - payment; } System.out.println(amount);
20th Aug 2021, 8:06 AM
David García Prados
+ 1
Another approach: Every month, if you pay 10% it remains the 90% First month: remainingAmount1 = amount * 0.9 Second month: remainingAmount2 = remainingAmount1 * 0.9 = amount * 0.9 * 0.9 And so on You can solve this using a loop or using Math.pow
20th Aug 2021, 8:06 AM
David García Prados
0
Mathematics is an exact science and the approach to it must be strictly in accordance with its laws. ☺️ You have been given a certain amount, for example $2000. You need to subtract 10% from it, that is, 10% of 100%, that is, $2000 is 100%. Agree, it will be strange 100% / 100% ☺️ To find 10% of an integer, you need to multiply the integer by a tenth of the integer, that is, 10/100, as a result we get: payment = amount * 10/100; 10/100 can be reduced by 10: payment = amount * 1/10, which corresponds to: payment = amount / 10; As a result of the calculation formula payment = amount * 10/100; and payment = amount / 10; are correct, as created according to mathematical rules. 2000 / 10 = 200 2000 - 200 = 1800 1800 / 10 = 180 1800 - 180 = 1620 1620 / 10 = 162 1620 - 162 = 1458
20th Aug 2021, 10:02 AM
Solo
Solo - avatar