Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The fragment of code which I'm about to post is from a program which is supposed to ask the user a series of multiple choice questions. The program will then keep score for each question and have a total which will be stored in another array. I will have multiple players playing this, so that's why i will need 2 arrays.

Here is what i have:

//Asks questions and stores score in array
public static int [] questions ()
{
    userinput=""; //input will be stored in here
    int total[]= new int[100];
    int score[]=new int[5];
    for(int i=0; i < ps.length; i++)
    {
        userinput=JOptionPane.showInputDialog(que[i]); //Outputs a question stored in   another array in another method.
        if (response.equals(ans[i])) //this compares the user input to the correct answer of the question, which is in another method.
        {
            JOptionPane.showMessageDialog(null,"You selected " + " " + ans[i] + " You were correct, 1 point!");
            score[i]=1;
            total[i]=total[i]+score[i];
        }
        else if(!response.equals(ans[i])) // If the answer isn't correct
        {
            score[i]=0; // I want to assign 0 for the question
            JOptionPane.showMessageDialog(null,"You're wrong!, The correct answer was "+ans[i]);
        }
    } // close loop
    return total; // return's this to another method which will do all of the other work
}

I seem to be having the problem here:

JOptionPane.showMessageDialog(null,"You selected " + " " + ans[i] + " You were correct, 1 point!");
score[i]=1;
total[i]=total[i]+score[i];    

I want to add 1 to each element in score[] if the answer is correct. Then i want to accumulate the total of score[] and store that in each element of total[]. I return total to another method which stores it in an array.

share|improve this question
1  
Your suspicious code is the same as totalscore[i]++. Besides, totalscore has 100 elems, whereas score has 15 elems. Why? –  Victor Sorokin Dec 6 '11 at 20:50
    
Sorry it was a copy and paste error. The only 2 arrays i'm working with are score[] and total[]. –  user1058452 Dec 6 '11 at 20:51
    
If you see any array's other than "total[]" or "score[]" ignore them! –  user1058452 Dec 6 '11 at 20:52
    
This still leaves my question unanswered, I guess... –  Victor Sorokin Dec 6 '11 at 20:53
    
Total[] has the value 100 because i will have multiple players, playing this. I just gave it a value which will be more than enough. –  user1058452 Dec 6 '11 at 20:54

1 Answer 1

Okay, so it seems you need to pass ordinal of current user in your method, so it can calculate correct position inside total array. Since it seems you want to aggregate total scores across multiple question/answer sessions, you need to pass total from outside:

public static void questions(int userOrdinal, int[] total) {
    final int questionsPerUser = 5;

    userinput = ""; //input will be stored in here
    for (int i = 0; i < questionsPerUser; i++) {
        userinput = JOptionPane.showInputDialog(que[i]); //Outputs a question stored in   another array in another method.
        if (response.equals(ans[i])) //this compares the user input to the correct answer of the question, which is in another method.
        {
            JOptionPane.showMessageDialog(null, "You selected " + " " + ans[i] + " You were correct, 1 point!");
            total[userOrdinal * questionsPerUser + i] = 1;
        } else if (!response.equals(ans[i])) // If the answer isn't correct
        {
            total[userOrdinal * questionsPerUser + i] = 0;
            JOptionPane.showMessageDialog(null, "You're wrong!, The correct answer was " + ans[i]);
        }
    } // close loop
}

Sorry, I still can't get why you need score array, since your initial code is the same as total[i]++ and you never read contents of score, only write to it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.