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.
totalscore[i]++
. Besides,totalscore
has 100 elems, whereasscore
has 15 elems. Why? – Victor Sorokin Dec 6 '11 at 20:50