mardi 28 février 2017

How can if statements be executed after an else statement has met its requirements?

public static void main(String args[]) {
    Scanner keyboard = new Scanner(System.in);
    int hankees, socks;

    out.print("Hankees and Socks scores?  ");
    hankees = keyboard.nextInt();
    socks = keyboard.nextInt();
    out.println();

    if (hankees > socks) {
        out.println("Hankees win...");
        out.print("Hankees: ");
        out.println(hankees);
        out.print("Socks:   ");
        out.println(socks);
    } else {
        out.println("It's a tie...");
        out.print("Hankees: ");
        out.println(hankees);
        out.print("Socks:   ");
        out.println(socks);
    }  if (socks > hankees) {
        out.println("Socks win...");
        out.print("Socks:   ");
        out.println(socks);
        out.print("Hankees: ");
        out.println(hankees);
    } 

    keyboard.close();
}

}

I am very new to Java, but I have noticed that Java codes tend to be executed regardless of the codes that follow if the conditions are met. So here I expected the else code to be executed ( in the case of socks> hankees) but the if statement that follows is properly taken into account. I would very much appreciate it if someone could explain why.

Aucun commentaire:

Enregistrer un commentaire