samedi 6 août 2016

Java Scanner input with if else Statement

Hi I'm new to java and trying to make a Quiz to practice. I wanna make a Question where the user has to combine words from to categories to pairs. Like A1 B4 C3 D2. What I did now is using an if else statement to check if the input is the correct answer, but it only works with 1A. For the others I can do 6 inputs, which is not what I want, and even if there's a correct one I don't get a point.

Any help appreciated!

public class HelloWorld {

public static void main(String[] args) {

    Scanner walther = new Scanner(System.in);

    String cro = "1A";
    String dan = "2C";
    String fin = "4D";
    String dut = "3F";
    String fre = "5B";
    String ger = "6E";
    int x = 0;


    if (cro.equalsIgnoreCase(walther.nextLine())){
        ++x;
        walther.close();
    }
    else if (dan.equalsIgnoreCase(walther.nextLine())){
        ++x;
        walther.close();
    }
    else if (fin.equalsIgnoreCase(walther.nextLine())){
        ++x;
        walther.close();
    }
    else if (dut.equalsIgnoreCase(walther.nextLine())){
        ++x;
        walther.close();
    }
    else if (fre.equalsIgnoreCase(walther.nextLine())){
        ++x;
        walther.close();
    }
    else if (ger.equalsIgnoreCase(walther.nextLine())){
        ++x;
        walther.close();
    }
    else {
        walther.close();
    }

System.out.println(x + " Point!");
}

}

Aucun commentaire:

Enregistrer un commentaire