tags:

views:

74

answers:

3

userAnswer[] holds the string of the answer the user types in and is comparing it to answers[] to see if they match up and then spits out correct or wrong. j is equal to the question number. So if j was question 6, answers[j] should refer to answers[6] right? Then userAnswer[6] should compare to answers[6] and match if its correct. But its giving me wrong answers and displaying the answer I typed as correct.

int abc, loopCount = 100;
int j = quesNum, overValue, forLoop = 100;

for (int loop = 1; loop < loopCount; loop++)
{
    aa = r.nextInt(10+1);
    abc = (int) aa;

    String[] userAnswer = new String[x];

    JOptionPane.showMessageDialog(null,abc);

    if(abc < x)
    {
        userAnswer[j] = JOptionPane.showInputDialog(null,"Question "+quesNum+"\n"+questions[abc]+"\n\nA: "+a[abc]+"\nB: "+b[abc]+"\nC: "+c[abc]+"\nD: "+d[abc]);

        if(userAnswer[j].equals(answers[j]))
        {
            JOptionPane.showMessageDialog(null,"Correct. \nThe Correct Answer is "+answers[abc]);
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Wrong. \n The Correct Answer is "+answers[abc]);
        }//else

    }//if

}//for
+4  A: 

Your indices are named poorly, and you mix them up yourself. You want to check answers[abc] and not answers[j] against userAnswer[j].

mihi
I know...but I was so far into the program Im not renaming them, but I will keep the variables better.
Nick Gibson
That fixed it! Thanks. Yes I really need to work on renaming variables to something useful. Thats a bad programming habit.
Nick Gibson
Jave IDEs can refractor the code for you. Changing a variable name is one of the many amazing things refactoring can do for you.
unholysampler
A: 

For question 6 you need answers[5] because arrays are indexed starting from 0. 0 would be question 1, 1 would be question 2, etc.

Ben Burnett
that depends. some programmers refer to the first question as question 0 :)
mihi
A: 

Your userAnswer array is scoped by the for loop. If you mean to use the values outside of this loop, you need to move the defintion outside of the loop. Otherwise, you can just use one answer string as right now you are not accessing the values a second time.

unholysampler