- 1

I can’t get it right, can someone help?

If I enter 1 it gives me result of 5.35 and that’s ok But if I enter input 3 for example, than my result is 14.98 And it should be 14.45 What’s the problem? https://code.sololearn.com/coq4v6Nn2VMX/?ref=app

3rd Feb 2021, 4:33 AM
Elad David
Elad David - avatar
5 odpowiedzi
+ 1
Elad David There is little change in your code. Try this import java.util.Scanner; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int kale = scanner.nextInt(); double price = kale * 5; double disCount = (price * 10) / 100; if (kale > 1) price = price - disCount; double tax = (price * 7) / 100; double totalPrice = price + tax; DecimalFormat f = new DecimalFormat("##.00"); System.out.println(f.format(totalPrice)); } }
3rd Feb 2021, 6:16 PM
A͢J
A͢J - avatar
+ 2
Elad David Because price is not double So output will be 14.98 see this explanation kale = 3 price = 5 * 3 = 15 disCount = 15 * 10 / 100 = 1; pricedis = 15 - 1 = 14.0 tax = 14.0 * 7 / 100 = 0.98; totalPrice = 14.0 + 0.98 = 14.98
3rd Feb 2021, 4:39 AM
A͢J
A͢J - avatar
+ 1
Elad David Just change declaration type of price (int to double) in case of 1 kale = 1 price = 5 * 1 = 5 disCount = 5 * 10 / 100 = 50 / 100 = 0; //because price is int not double when it is double then 0.50 pricedis = 5 - 0 = 5.0 tax = 5.0 * 7 / 100 = 35.0 / 100 = 0.35; totalPrice = 5.0 + 0.35 = 5.35
3rd Feb 2021, 10:24 AM
A͢J
A͢J - avatar
0
ok I undersyand the math now. but still dont know what to change in the code
3rd Feb 2021, 10:15 AM
Elad David
Elad David - avatar
0
Im sorry but its not working 😔🤷🏻‍♂️
3rd Feb 2021, 5:30 PM
Elad David
Elad David - avatar