mercredi 21 juin 2017

how to compare string with string in if and else java

I just need some help with what should be an easy problem, but i can't find any information on this specifically. I am doing a quiz program, and everything in the program works except for the error message that I want when the user input something other than the alternatives.

If the answer is wrong it works, if its right it works, but not when user puts in something else than 1 or x, or 2 ( I want them to get an error message)

for(int i = 0; i<12; i++)
        {
            System.out.println(Questions[i][0]);
            System.out.println("Ditt svar:");
            selection = Input.next();

            if(selection.equals(Questions[i][1]))
            {
                points++;
                System.out.println("Rätt! Dina poäng: " + points);
            }
            else if(selection!=="x" || selection==!"1" || selection!=="2")
            {
                System.out.println("Du måste välja något av svarsalternativen, försök igen!");
                i--;
            }

            else
            {
                System.out.println("Fel svar!, Dina poäng:" + points);

            }

        }

I have tried multiple different methods, but the error message comes up even when the user pick the wrong answer. And I only want it to come up when he enter something else than 1 x or 2. While we are on it I would also be extremely glad if I can have an option to quit the program by pressing q(implemented in the if else)

Aucun commentaire:

Enregistrer un commentaire