Date: 18/06/25 - 05:54 AM   48060 Topics and 694399 Posts

Author Topic: Anybody know Java?  (Read 2364 times)

April 30, 2006, 10:44:23 PM
Read 2364 times

badke

  • Cub

  • Offline

  • 78
    • Wildcat Victory
I'm new with Java and programming all together. I'm trying to put together a quiz program using JOptionPane. I want the questions to be random and the answers random as well. So that when a user takes the test the questions are never in the same order, and the answers to each question are never in the same order. But I'm lost on how to do it, this is the code I have so far:

Code: [Select]
import javax.swing.JOptionPane;

class Quiz
{
    public static String [] question;
    public static String [][] answer;
    public static int qCount = 0;

    public static String [] loadQuestions( )
    {
        String [] q = new String [4];
        // The questions
        q[0] = "What is...?";
        q[1] = "Where is...?";
        q[2] = "Who is...?";
        q[3] = "Why is...?";

        return q;
    }

    public static String [][] loadAnswers( )
    {
        String [][] a = new String[4][];
        // The answers
        a[0] = new String [] { "A", "B", "C", "D" };
        a[1] = new String [] { "A", "B", "C", "D" };
        a[2] = new String [] { "A", "B", "C", "D" };
        a[3] = new String [] { "A", "B", "C", "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 );
    return userAnswer.toString( );
    }

    public static void main ( String [] args )
    {
    question = loadQuestions( );
            answer = loadAnswers( );
            for ( int k = 0; k<4; k++ )
                System.out.println( askOne( k ) );
    }
}

How in the hell do I make the questions and each question's answers display randomly? :confused:

May 01, 2006, 01:17:20 AM
Reply #1

swish1

  • Classless Cat
  • Cub

  • Offline
  • ***

  • 1168
i took cis 200 5 years ago and have effectively blocked it from my memory so i dont think id be any help.  i do remember things about for loops and strings and random pointless crap that for some reason the alcohol hasnt rid me of yet.

May 01, 2006, 04:39:17 AM
Reply #2

fatty fat fat

  • Premium Member
  • Hall of Fame

  • Offline
  • *******

  • 29013
  • Personal Text
    The very best.
I just crapped my pants.
It is a tragedy because now, we have at least an extra month without Cat football until next year. I hate wasting my life away but I can hardly wait until next year.

May 01, 2006, 06:46:59 AM
Reply #3

Saulbadguy

  • Guest
JavaCat is a good guy.

May 01, 2006, 10:10:49 AM
Reply #4

jeffy

  • Scout Team Wildcat

  • Offline
  • **

  • 7000
  • Personal Text
    ku Swallows

May 01, 2006, 12:02:37 PM
Reply #5

kougar24

  • Scout Team Wildcat

  • Offline
  • **

  • 6966
  • Personal Text
    shame on you, non-believers
pbadke, I'll help you out when I get some time here in awhile.

May 01, 2006, 01:29:07 PM
Reply #6

badke

  • Cub

  • Offline

  • 78
    • Wildcat Victory
Excellent, thanks.

May 01, 2006, 01:56:43 PM
Reply #7

badke

  • Cub

  • Offline

  • 78
    • Wildcat Victory
Ok, so I think I have the basic idea down. This is the code:

Code: [Select]
import javax.swing.JOptionPane;

class Quiz
{
    public static String [] question;
    public static String [][] answer;
    public static int qCount = 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 [] { "Q1 A", "Q1 B", "Q1 C", "Q1 D" };
        a[1] = new String [] { "Q2 A", "Q2 B", "Q2 C", "Q2 D" };
        a[2] = new String [] { "Q3 A", "Q3 B", "Q3 C", "Q3 D" };
        a[3] = new String [] { "Q4 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 );
    return userAnswer.toString( );
    }

    public static void main ( String [] args )
    {
    question = loadQuestions( ); // Load the questions
            answer = loadAnswers( ); // Load the answers
   
    /* Randomize the questions in the quiz */
    int lastQuestion = question.length-1;
    while( lastQuestion > 0 )
    {
    int r = (int) (Math.random() * (lastQuestion - 0 + 1) ) + 0;
    question[r] = question[lastQuestion];
    answer[r] = answer[lastQuestion];
    lastQuestion--;
    }

    /* Ask the questions */
            for ( int k = 0; k<4; k++ )
                System.out.println( askOne( k ) );
    }
}

It randomizes the questions. But it repeats one of the questions and doesn't include another one. So I'll have one question that's repeated and another question that is never asked.

May 01, 2006, 02:00:18 PM
Reply #8

badke

  • Cub

  • Offline

  • 78
    • Wildcat Victory
Umm, and now looking at my random number, I dunno what I did there lol.

May 01, 2006, 04:35:49 PM
Reply #9

Super PurpleCat

  • Cub

  • Offline

  • 2211
  • Personal Text
    Since 1996
If I remember right (been a looong time since I did any Java work), java.util contains a class you can use to easily shuffle a collection.  I mean, that's all you're trying to do here is reorder a simple one-dimensional array. 

So my advice, do some googling on java.util and see what you come up with for shuffling an array.


May 01, 2006, 06:51:40 PM
Reply #10

kougar24

  • Scout Team Wildcat

  • Offline
  • **

  • 6966
  • Personal Text
    shame on you, non-believers

May 01, 2006, 06:54:30 PM
Reply #11

AzCat

  • Classless Cat
  • Scout Team Wildcat

  • Offline
  • ***

  • 7320
That's the trouble with kids these days, they need a library to do all of their work for them.   :jerkoff:
Ladies & gentlemen, I present: The Problem

May 01, 2006, 08:32:54 PM
Reply #12

fatty fat fat

  • Premium Member
  • Hall of Fame

  • Offline
  • *******

  • 29013
  • Personal Text
    The very best.
If I remember right (been a looong time since I did any Java work), java.util contains a class you can use to easily shuffle a collection.  I mean, that's all you're trying to do here is reorder a simple one-dimensional array. 

So my advice, do some googling on java.util and see what you come up with for shuffling an array.



spring 2005 = a long time back?  :)
It is a tragedy because now, we have at least an extra month without Cat football until next year. I hate wasting my life away but I can hardly wait until next year.

May 02, 2006, 01:20:16 PM
Reply #13

badke

  • Cub

  • Offline

  • 78
    • Wildcat Victory
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?

Code: [Select]
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:

Code: [Select]
if ( userAnswer == "\n*" )
points++;

So, anybody know how I search for \n in userAnswer?

May 02, 2006, 07:33:42 PM
Reply #14

kougar24

  • Scout Team Wildcat

  • Offline
  • **

  • 6966
  • Personal Text
    shame on you, non-believers
I would advise against trying to manipulate an JOptionPane for the user's answer. Strings or ints are much easier to manipulate.

May 02, 2006, 08:13:55 PM
Reply #15

Super PurpleCat

  • Cub

  • Offline

  • 2211
  • Personal Text
    Since 1996
If I remember right (been a looong time since I did any Java work), java.util contains a class you can use to easily shuffle a collection.  I mean, that's all you're trying to do here is reorder a simple one-dimensional array. 

So my advice, do some googling on java.util and see what you come up with for shuffling an array.



spring 2005 = a long time back?  :)

I know English is like your third language, but "Java work" is pretty clear I believe.