Hoy encontré este rompecabezas. Obviamente, este no es el estilo correcto, pero todavía tengo curiosidad de por qué no sale ningún resultado.¿Por qué mi sentencia if se comporta de esta manera?
int x = 9;
int y = 8;
int z = 7;
if (x > 9) if (y > 8) System.out.println("x > 9 and y > 8");
else if (z >= 7) System.out.println("SHOULD OUTPUT THIS x <= 9 and z >= 7");
else
System.out.println("x <= 9 and z < 7");
Lo anterior no tiene salida cuando se ejecuta. Pero, cuando agregamos entre corchetes para el enunciado if, de repente la lógica se comporta como esperaba.
int x = 9;
int y = 8;
int z = 7;
if (x > 9) {
if (y > 8) System.out.println("x > 9 and y > 8");
}
else if (z >= 7) System.out.println("SHOULD OUTPUT THIS x <= 9 and z >= 7");
else
System.out.println("x <= 9 and z < 7");
Esto da salida a "DEBERÍA Salida este x < = 9 y z> = 7". ¿Que esta pasando aqui?
Gracias!
jaja ... El 'else' se aplica al nivel de anidación más interno. – Mysticial