+ 1

Halloween candy (code coach riddle) - who can tell me whats wrong with this code? - JAVA

I'm trying to solve Code Coach task (easy). My way of thinking here is: 1. Get no. of houses 2. Extract percentage 3. Round the outcome 4. Produce result Got it on code playground, where I added additional outputs for monitoring. It works nice with any no. of houses, but won't work as solution (I modify it beforehand to get only required percentage outcome after rounding). Why? CODE: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int houses = input.nextInt(); //your code goes here for(int x=3; x<=houses; x++) { double no1 = (2/(double)x); double no2 = no1*1000; int num = (int)no2; System.out.println(x); System.out.println(no1); System.out.println(no2); System.out.println(num); if (num % 10 >= 5) {System.out.println((num/10) + 1);} else {System.out.println(num/10);} System.out.println(""); } } }

17th Jun 2020, 11:41 AM
jasco
jasco - avatar
5 ответов
+ 2
You have to do something like this, import java.util.Scanner; import java .text.*; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int houses = input.nextInt(); var chances=0.0; double no1 = (2/(double)houses); double no2 = no1*1000; chances=(no2/10.0); DecimalFormat df=new DecimalFormat("#"); System.out.println(df.format(Math.ceil(chances))); } } Don't know why I use DecimalFormat, instead you can use type casting.
17th Jun 2020, 1:38 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
@jasco tell me the error?
17th Jun 2020, 1:50 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
The code before you edited it, gave 3 errors, but worked perfectly fine after your editing. Thank you. I'll learn about the .format, Math.ceil and analyse how it all works
17th Jun 2020, 2:00 PM
jasco
jasco - avatar
0
Won't work either unfortunately :/
17th Jun 2020, 1:48 PM
jasco
jasco - avatar
0
This code works!!!!: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int houses = input.nextInt(); //your code goes here int chances = 200/houses; if(200%houses!=0) System .out .println (chances+1); else System .out .println (chances); } }
5th Apr 2021, 5:20 AM
Mario Enrique Zayas Leal
Mario Enrique Zayas Leal - avatar