-2

I have to make the "Who wants to be a millionaire" game. The game has to work with file (writing/ creating a user file with all the info). The questions are picked randomly from a txt file and saved in "intrebareSIraspuns[6][15]"(6 -> 1 question, 4 answers, 1 correct answer; 15 -> questions). The questions are named "questionNr" and it's incremented after each correct answer. My problem is when the user has to give the correct answer for the question. The user has to type in the console the the corresponding letter of the answer (A, B, C, D), but when the correct answer is typed the result is always wrong. I will put the code for the correct question verification, which I've made it a little more longer just to check where is the problem, and the output of the console.

Here's code:

    /*                      *
     *                      *
     *     ANSWER CHECK     *
     *                      *
     *                      *
     */

public int check()
{
    Scanner input=new Scanner(System.in);

    int ret=0;

    System.out.println("Type your answer: ");
    userAnswer=input.next();


    if(intrebareSIraspuns[5][questionNr].equals(intrebareSIraspuns[1][questionNr]))
    {
        ret=1;
    }

    if(intrebareSIraspuns[5][questionNr].equals(intrebareSIraspuns[2][questionNr]))
    {
        ret=2;
    }

    if(intrebareSIraspuns[5][questionNr].equals(intrebareSIraspuns[3][questionNr]))
    {
        ret=3;
    }

    if(intrebareSIraspuns[5][questionNr].equals(intrebareSIraspuns[4][questionNr]))
    {
        ret=4;
    }

    System.out.println("Return = " + ret);
    System.out.println("User Answer =" + userAnswer + ".");

    if((userAnswer=="A") && (ret==1))
    {
        answerVar=1;
        System.out.println("The corect answer is: A. " + intrebareSIraspuns[1][questionNr]);
    }
    else
        if((userAnswer=="B") && (ret==2))
        {
            answerVar=2;
            System.out.println("The corect answer is: B. " + intrebareSIraspuns[2][questionNr]);
        }
        else
            if((userAnswer=="C") && (ret==3))
            {
                answerVar=3;
                System.out.println("The corect answer is: C. " + intrebareSIraspuns[3][questionNr]);
            }
            else
                if((userAnswer=="D") && (ret==4))
                {
                    answerVar=4;
                    System.out.println("The corect answer is: D. " + intrebareSIraspuns[4][questionNr]);
                }
                else
                {
                    answerVar=5;
                    System.out.println("Your answer is wrong!");
                    if(ret==1)
                    {
                        answerVar=6;
                        System.out.print(" The corect answer was: A. " + intrebareSIraspuns[1][questionNr] + "\n");
                    }
                    else
                        if(ret==2)
                        {
                            answerVar=7;
                            System.out.print(" The corect answer was: B. " + intrebareSIraspuns[2][questionNr] + "\n");
                        }
                        else
                            if(ret==3)
                            {
                                answerVar=8;
                                System.out.print(" The corect answer was: C. " + intrebareSIraspuns[3][questionNr] + "\n");
                            }
                            else
                                if(ret==4)
                                {
                                    answerVar=9;
                                    System.out.print(" The corect answer was: D. " + intrebareSIraspuns[4][questionNr] + "\n");
                                }
                }
    input.close();
    return answerVar;
}

And the console output is:

0081 Whose return to 'EastEnders' in 2004 sparked a 560 megawatt power surge on the national grid?

    A. Filthy Fred

    B. Smutty Sam

    C. Grubby Gordon

    D. Dirty Den

ANSWER. Dirty Den

Type your answer: 
D

Return = 4

User Answer =D.

Your answer is wrong!

 The corect answer was: D. Dirty Den
Sam
  • 7,252
  • 16
  • 46
  • 65
Flavius
  • 67
  • 1
  • 9

2 Answers2

2

Strings should always compare with equals.

I wrote reason here :Why doesn’t == work on String?

  if((userAnswer.equals("B")) && (ret==2)).

And change in remaining places also.

And also consider using char in your case. Not the String. As you are dealing with a Single letter using a char or an Enum increases your programs performance.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Use equals() instead of == for comparing Strings

Reason

equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects!. That means, the contents of the objects. Whereas the == operator is expected to check the actual object instances are same or not.

i.e,

if((userAnswer.equals("B")) && (ret==2))
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55