-1

I have an array of questions with an array of corresponding answers. I get the user to answer the question, then compare the answer to the real answer using an if statement. Even if the answer is the correct thing, it still displays the output as incorrect. Please help.

Scanner input = new Scanner(System.in);

    int inputOne;
    int inputTwo;

    String answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, answer11, answer12;


    String[] questions = {"How many continents are there?", "What is the capital of Canada?", "What is the largest country in the world?", "What is the largest ocean in the world?", "How many oceans are there in the world?", "How many countries make up Africa?", "How many countries in the world begin with the word United?", "Where is Milan?", "What is the least populated US state?", "What is the capital of Australia?", "How many countries begin with the letter J?", "Which country has the most lakes in the world?"};                                                                                                                                                                                                                                                                                          //

    String[] answers = {"7", "Ottawa", "Russia", "Pacific", "5", "54", "3", "Italy", "Wyoming", "Canberra", "3", "Finland"};                                                                                                            





    System.out.println("Enter the row and column: ");

    inputOne=input.nextInt();
    inputTwo=input.nextInt();



    if (inputOne < 0 || inputTwo < 0 || inputOne > 2 || inputTwo > 3) {
        System.err.println("Invalid input. Input a number between 0 - 2 for the row, and a number between 0 - 3 for the column.");
    }   

    if (inputOne == 0 && inputTwo == 0) {
        System.out.println("You chose 500.");
        System.out.println(questions[11]);
        answer1=input.next();
        if (answer1 == answers[11]) {
            System.out.println("Correct!");
        }
        else {
            System.err.println("Wrong.");
        }
    }

1 Answers1

0

If strings are equal you want to use

str1.equals(str2);

Using == checks if both variable point to the same memory location. Also, it is better to use a Dictionary for Question and answer type problems, where the question is the key and the answer is the value.