vendredi 29 juin 2018

Why is scanner skipping the if else statement

Pretty straightforward code, if I change string K to "Bye" I receive an error, only the if else structure works, not the if else if else. Please help me understand why.

public static void main(String [] args){
    String k = "Hi";
    Scanner scnr = new Scanner(k);
    if (scnr.next().equals("Hi")){
        System.out.println("Yes");
    }
    else if (scnr.next().equals("Bye")){
    System.out.println("No");
    }
    else {
    System.out.println("Maybe");
    }
}

Aucun commentaire:

Enregistrer un commentaire