0
Popsicle problem: why does my code not work for test 2 & 5?
int siblings = input.nextInt(); int popsicles = input.nextInt(); //your code goes here if (popsicles>siblings) { float x = popsicles / siblings; if (x == Math.ceil(x) && x == Math.floor(x)) { System.out.println("give away"); } else System.out.println("eat them yourself"); } else System.out.println("eat them yourself"); }
4 Antworten
+ 6
You need to use float variables for division to avoid losing decimals.
Also, you can give popsicles to your siblings if you have same number of siblings and popsicles.
float siblings = input.nextFloat();
float popsicles = input.nextFloat();
//your code goes here
if (popsicles>=siblings) {
...
+ 3
Sean Lim ,
we only need to check if popsicles can be evenly divided by siblings (without a remainder) by using a modulo division:
....
if (popsicles % siblings == 0) {
System.out.println(".......");
}
else {
System.out.println(".......");
}
....
0
Please always tag the language you're asking about.
https://code.sololearn.com/W3uiji9X28C1/?ref=app
0
popsicles%siblings==0