...

View Full Version : Need help with a quiz program to ask questions randomly



shatgid
03-29-2012, 09:28 PM
I am writing a java program to ask the user 5 random questions from 10 questions stored in an array, output if the answer is correct or incorrect and not to ask the same question more than once. Everything works except for the only asking the question only once. I can't figure out how to do this, can someone please help me?



package Quiz; //assigns package name
import java.util.Scanner; //adds Scanner utility from java library
import java.util.Random; //adds Random utility from java library

public class Quiz //start Quiz class
{
String question; //initializes question string
String answer; //initializes answer variable
int correct=0, number; //initializes scoring variables
Quiz[] quizBank = new Quiz[10]; //initializes quizBank array

public static void main(String[] args) //start of main
{
Quiz bank = new Quiz(); //creates new Quiz object
bank.bankList(); //assigns name of portion of program to build the collection of questions and answers
bank.askQuestion(); //assigns name of portion of program to ask the questions
} //end main

public void bankList() //start of bankList
{
quizBank[0] = new Quiz(); //Creates new object
quizBank[0].question = "The smallest prime number"; //Initialize object variables
quizBank[0].answer = "2"; //Initialize object variables

quizBank[1] = new Quiz(); //Creates new object
quizBank[1].question = "Area of triangle with base = 4 and height = 3"; //Initialize object variables
quizBank[1].answer = "6"; //Initialize object variables

quizBank[2] = new Quiz(); //Creates new object
quizBank[2].question = "Area of square with side = 5"; //Initialize object variables
quizBank[2].answer = "25"; //Initialize object variables

quizBank[3] = new Quiz(); //Creates new object
quizBank[3].question = "Square root of 144"; //Initialize object variables
quizBank[3].answer = "12"; //Initialize object variables

quizBank[4] = new Quiz(); //Creates new object
quizBank[4].question = "No. of states in US"; //Initialize object variables
quizBank[4].answer = "50"; //Initialize object variables

quizBank[5] = new Quiz(); //Creates new object
quizBank[5].question = "No. of continents in the world"; //Initialize object variables
quizBank[5].answer = "7"; //Initialize object variables

quizBank[6] = new Quiz(); //Creates new object
quizBank[6].question = "In which year did man land on the moon"; //Initialize object variables
quizBank[6].answer = "1969"; //Initialize object variables

quizBank[7] = new Quiz(); //Creates new object
quizBank[7].question = "How many colors in a rainbow"; //Initialize object variables
quizBank[7].answer = "7"; //Initialize object variables

quizBank[8] = new Quiz(); //Creates new object
quizBank[8].question = "How many colors in the US flag"; //Initialize object variables
quizBank[8].answer = "3"; //Initialize object variables

quizBank[9] = new Quiz(); //Creates new object
quizBank[9].question = "Square of 25"; //Initialize object variables
quizBank[9].answer = "625"; //Initialize object variables
} //end of bankList

public void askQuestion() //start of askQuestion
{
Scanner input = new Scanner(System.in); //prepare to read input from keyboard
System.out.println("********************************"); //prints heading top border
System.out.println(" Welcome to my Quiz Application"); //prints heading
System.out.println("********************************"); //prints heading bottom border

for (number=1; number<6; number++) //start of counter for loop
{
Random draw = new Random(); //creates new random object
int choose = draw.nextInt(5); //randomly chooses a question
System.out.printf("%d. %s?%n", number, quizBank[choose].question); //prints question
String entered = input.nextLine(); //read input

if (entered.compareTo(quizBank[choose].answer)==0) //checks the users input
{
System.out.println("*** Correct! ***"); //prints correct response
correct = correct + 1; //counts number of correct answers
}
else //start of response for wrong answers
System.out.println("--- Incorrect! ---"); //print the incorrect response
} //end of counter for loop

System.out.println("*******************"); //prints footer top border
System.out.printf(" Your score is %d/%d%n", correct, number-1); //prints results
System.out.println("*******************"); //prints footer bottom border
} //end of askQuestion

} //end public class

Fou-Lu
03-30-2012, 05:54 PM
I'd shuffle it instead of using Random. Problem with Random is you will need to keep asking it to give you something new and checking to see if its already given it to you.
To shuffle, you'd use collections:


List<Quiz> quizList = Arrays.asList(quizBank);
Collections.shuffle(quizList);
int iSize = quizList.size();
int iSelect = 5;
for (int i = 0; i < iSize && i < iSelect; ++i)
{
System.out.println(quizList.get(i).question);
}

For example.

Otherwise, there are a few approaches. You can clone the existing array, and as you pull from it set it to null. Simply loop until you find something. Another is to create an array of int that tracks the position of one pulled. If it matches the random selection, try again.
Random is wrong here though, that should probably be .nextInt(quizBank.length).

shatgid
04-01-2012, 04:55 AM
I appreciate your help, but I'm sorry to say your pretty much talking over my head. I don't really even follow your code. I am new to java and really only know the stuff in my code (barely). I am in an intro-programing class and we flew through java in 5 weeks, so I don't know much more than what you see. My textbook is no help, the teacher isn't following it at all, and I've been searching the net for something similar with no luck.

shatgid
04-03-2012, 12:38 AM
I'm not quite sure how I did it but I got it to work with your code and some searching on the net. I don't fully understand it, but it works. Thank you!!
Below is my code, in case anyone else needs something similar.



package Quiz; //assigns package name
import java.util.Arrays; //adds Arrays utility from java library
import java.util.List; //adds List utility from java library
import java.util.Scanner; //adds Scanner utility from java library
import java.util.Collections; //adds Collections utility from java library

public class Quiz //start Quiz class
{
String question; //initializes question string
String answer; //initializes answer string
int correct=0, number; //initializes scoring variables
Quiz[] quizBank = new Quiz[10]; //initializes quizBank array
List<Quiz> quizList = Arrays.asList(quizBank); //sets the array as a list

public static void main(String[] args) //start of main
{
Quiz bank = new Quiz(); //creates new Quiz object
bank.bankList(); //assigns name of portion of program to build the collection of questions and answers
bank.askQuestion(); //assigns name of portion of program to ask the questions
} //end main

public void bankList() //start of bankList
{
quizBank[0] = new Quiz(); //Creates new object
quizBank[0].question = "The smallest prime number"; //Initialize object variables
quizBank[0].answer = "2"; //Initialize object variables

quizBank[1] = new Quiz(); //Creates new object
quizBank[1].question = "Area of triangle with base = 4 and height = 3"; //Initialize object variables
quizBank[1].answer = "6"; //Initialize object variables

quizBank[2] = new Quiz(); //Creates new object
quizBank[2].question = "Area of square with side = 5"; //Initialize object variables
quizBank[2].answer = "25"; //Initialize object variables

quizBank[3] = new Quiz(); //Creates new object
quizBank[3].question = "Square root of 144"; //Initialize object variables
quizBank[3].answer = "12"; //Initialize object variables

quizBank[4] = new Quiz(); //Creates new object
quizBank[4].question = "No. of states in US"; //Initialize object variables
quizBank[4].answer = "50"; //Initialize object variables

quizBank[5] = new Quiz(); //Creates new object
quizBank[5].question = "No. of continents in the world"; //Initialize object variables
quizBank[5].answer = "7"; //Initialize object variables

quizBank[6] = new Quiz(); //Creates new object
quizBank[6].question = "In which year did man land on the moon"; //Initialize object variables
quizBank[6].answer = "1969"; //Initialize object variables

quizBank[7] = new Quiz(); //Creates new object
quizBank[7].question = "How many colors in a rainbow"; //Initialize object variables
quizBank[7].answer = "7"; //Initialize object variables

quizBank[8] = new Quiz(); //Creates new object
quizBank[8].question = "How many colors in the US flag"; //Initialize object variables
quizBank[8].answer = "3"; //Initialize object variables

quizBank[9] = new Quiz(); //Creates new object
quizBank[9].question = "Square of 25"; //Initialize object variables
quizBank[9].answer = "625"; //Initialize object variables
Collections.shuffle(quizList); //shuffles the list
} //end of bankList

public void askQuestion() //start of askQuestion
{
Scanner input = new Scanner(System.in); //prepare to read input from keyboard
System.out.println("********************************"); //prints heading top border
System.out.println(" Welcome to my Quiz Application"); //prints heading
System.out.println("********************************"); //prints heading bottom border

for (number=1; number<6; number++) //start of counter for loop
{
System.out.printf("%d. %s?%n", number, quizBank[number].question); //prints question
String entered = input.nextLine(); //read input

if (entered.compareTo(quizBank[number].answer)==0) //checks the users input
{
System.out.println("*** Correct! ***"); //prints correct response
correct = correct + 1; //counts number of correct answers
} //end of if
else //start of response for wrong answers
System.out.println("--- Incorrect! ---"); //print the incorrect response
} //end of counter for loop

System.out.println("*******************"); //prints footer top border
System.out.printf(" Your score is %d/%d%n", correct, number-1); //prints results
System.out.println("*******************"); //prints footer bottom border
} //end of askQuestion

} //end public class



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum