mercredi 1 septembre 2021

Why does else if and else act different here?

When I do this,

      if(a>=b && a>=c) {
        max = a;

    } else if (b>=a && b>=c) {
        max = b;

    } else if (c>=a && c>=b) {
        max = c;

    }
    System.out.println(max);

It gives an error (java: variable max might not have been initialized), but when i do it like this,

    if(a>=b && a>=c) {
        max = a;

    } else if (b>=a && b>=c) {
        max = b;

    } else {
        max = c;

    }
    System.out.println(max);

It works. Why is it like that?

Aucun commentaire:

Enregistrer un commentaire