I figured out a way to do it just by swapping both the question and answer arrays at the same time.
Now I'm trying to figure out how to tally points in the quiz. One way I could think of was to just put \n in front of the correct answer in the answer array. Then search the user's answer to see if \n is in it, if it is, give them a point. However, I'm not sure how to search for \n exactly, any ideas?
import javax.swing.JOptionPane;
import java.util.Random;
class Quiz
{
public static String [] question;
public static String [][] answer;
public static int qCount = 0;
public static int points = 0;
public static int attempted = 0;
public static String [] loadQuestions( )
{
// instantiate the question array
String [] q = new String [4];
// store the questions
q[0] = "What is...?";
q[1] = "Where is...?";
q[2] = "Who is...?";
q[3] = "Why is...?";
return q;
}
public static String [][] loadAnswers( )
{
// instantiate the row vector
String [][] a = new String[4][];
// store the answers
a[0] = new String [] { "\nQ1 A", "Q1 B", "Q1 C", "Q1 D" };
a[1] = new String [] { "\nQ2 A", "Q2 B", "Q2 C", "Q2 D" };
a[2] = new String [] { "\nQ3 A", "Q3 B", "Q3 C", "Q3 D" };
a[3] = new String [] { "\nQ4 A", "Q4 B", "Q4 C", "Q4 D" };
return a;
}
public static String askOne( int item )
{
Object userAnswer;
userAnswer = JOptionPane.showInputDialog( null, question[item], "Question " + ++qCount, JOptionPane.QUESTION_MESSAGE, null, answer[item], answer[item][0] );
if ( userAnswer == null )
System.exit( 0 );
if ( userAnswer == "\n*" )
points++;
attempted++;
return userAnswer.toString( );
}
public static void main ( String [] args )
{
// Load the questions and answers
question = loadQuestions( );
answer = loadAnswers( );
// Randomize the questions in the quiz
int lastQuestion = question.length-1;
while( lastQuestion > 0 )
{
int r = (int) (Math.round(Math.random()*lastQuestion));
String tempQuestion = question[r]; question[r] = question[lastQuestion]; question[lastQuestion] = tempQuestion;
String [] tempAnswer = answer[r]; answer[r] = answer[lastQuestion]; answer[lastQuestion] = tempAnswer;
lastQuestion--;
}
// Randomize the order of the answers to each question
for( int k = 0; k<answer.length; k++)
{
int lastAnswer = question.length-1;
while( lastAnswer > 0 )
{
int r = (int) (Math.round(Math.random()*lastAnswer));
String temp = answer[k][r]; answer[k][r] = answer[k][lastAnswer]; answer[k][lastAnswer] = temp;
lastAnswer--;
}
}
// Ask the questions
for ( int k = 0; k<question.length; k++ )
System.out.println( askOne( k ) );
JOptionPane.showMessageDialog( null, "You attempted a total of " + attempted + " questions. You got " + points + " correct. ", "Results Of Your Quiz", JOptionPane.PLAIN_MESSAGE );
}
}
It'd be nice of Java had a wildcard function so I could just use:
if ( userAnswer == "\n*" )
points++;
So, anybody know how I search for \n in userAnswer?