+ 4

Java question Can anyone help me with the test case situation in that problem like 3 test case is pass but other are not 🤦🤦

https://www.sololearn.com/coach/44?ref=app import java.util.*; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in) ; double ni = sc.nextInt(); float price , dis ; if(ni >1){ dis = (float )((5 * ni)*(3.7/100)); price = (float )((5*ni) - dis) ; }else { dis = (float )((5.00*ni)*(7.00/100)); price =(float )((5.00*ni)+dis) ; } System.out.println(price); } } This is my code and like the question it should be 3% reduction in the price but with that I can only run one test case and with 3.7 % it becomes 3 test cases.

7th Mar 2025, 12:14 PM
Tarun Thakur
Tarun Thakur - avatar
12 Réponses
0
Lothar Output Format can be a factor but there is one more like if you see if input is 3 then the price should be 14.55 but it is 14.45 which is 3.66% deduction not 3%
8th Mar 2025, 9:32 PM
Tarun Thakur
Tarun Thakur - avatar
+ 5
Tarun Thakur, the task is the code coach exercise *kaleidoscopes*. > we still need to see your code attempt. the link you provided does not enable us to see your code. so just copy your code and paste it here.
7th Mar 2025, 6:19 PM
Lothar
Lothar - avatar
+ 4
Mention the title of the task. Show your code.
7th Mar 2025, 1:01 PM
Lisa
Lisa - avatar
+ 4
Tarun Thakur , here a hint that may help you to fix the issue. > comparing the result from your code and a code that passes all test cases shows a difference for some input values. input `3` items => 14.445 should be => 14.45 input `5` items => 24.075 should be => 24.08 ... so in some cases your output has *3* digital places, the working code has *2* digital places. may be you have overseen this part of the task description: > Output Format: A number that represents the total purchase price to *two* decimal places.
8th Mar 2025, 7:51 PM
Lothar
Lothar - avatar
+ 3
Tarun Thakur If you are talking about the code coach or any code problem in the lesson, then you have to write a code that satisfy with all the test cases. And if you are facing any issue in your code then you can share your code here with brief description.
7th Mar 2025, 3:57 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 3
It took me a while to figure out what your logic is here, but now I think I get it. You might have to make sure your output is the correct format and that's it. Have you copied your code into the code playground and run some numbers yourself?
8th Mar 2025, 7:52 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Remember, we are trying to help. We're not saying you're wrong, we all want you to get those green ticks! Something has to change with the code because you're not getting those ticks. Why not try some of these suggestions?
9th Mar 2025, 11:47 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Bro, check the logic again in else. It seems that there is a miscalc in the discount, even increasing the price 🫠. Also, if this = 1, which rule should he be subjected to? Btw, 3.7% is not the usual number, crosscheck is the same problem. Debug using a failed sample case, you will definitely find an anomaly
9th Mar 2025, 11:58 AM
—DNS—
—DNS— - avatar
0
Ausgrindtube It is in the correct format like I have changed many times 😕
8th Mar 2025, 4:18 PM
Tarun Thakur
Tarun Thakur - avatar
0
Corected code: ```java import java.util.*; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double ni = sc.nextInt(); float price, dis; if(ni > 1){ dis = (float)((5 * ni)*(3/100)); // 3% discount price = (float)((5*ni) - dis); }else { dis = (float)((5.00*ni)*(7.00/100)); // 7% extra charge price =(float)((5.00*ni) + dis); // correctly adding extra charge } System.out.println(String.format("%.2f", price)); // printing price with 2 decimal places } } ``` Now your code should work correctly and pass all test cases. Do you want me to explain what was wrong with the original 3.7% discount?
8th Mar 2025, 5:58 PM
ZAHID AHMAD NAJAR
ZAHID AHMAD NAJAR - avatar
0
𝐙𝐚𝐡𝐢𝐃 𝐀𝐡𝐦𝐚𝐃 𝐍𝐚𝐣𝐚𝐑 Again it's not working and with your code only 1 test case is passed
8th Mar 2025, 7:12 PM
Tarun Thakur
Tarun Thakur - avatar