Java Question (programmers?)?

public class math {
public static void main (String [] args) {
if ((1/(1+1))*2>0)
System. out. println ("true");
else
System. out. println ("false");

}
}

Can anyone tell me why this prints the result false? 1+1 = 2. 1/2 = 0.5 * 2 = 1 which is definitely superior to 0 That should be pinting "true". Why isn't it?

Updates:
3mo Many thanks everyone! That definitely helped, should have thought of that!

0|0
0|3

Most Helpful Guy

  • I think the variable has to be set to another type of variable that isn't just an integer, like a double.

    Otherwise 1/2 will round down to zero, and the comp will do 0 รท 0, which is zero, which make the if/then logic follow the false case, which prints false.

    0|1
    0|0

What Girls Said 0

No girls shared opinions.

What Guys Said 2

  • Because 1 / 2 is 0, because you're dividing integer with integer

    You're looking for this:

    if ((((double)1) / (1 + 1)) * 2 > 0) {

    0|4
    0|0
  • it's like @Mesonfielde said

    you need to cast at least on of the arguments to a double or float

    0|1
    0|0
Loading...