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
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