Anda di halaman 1dari 2

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 coll
ection of questions and answers
bank.askQuestion(); //assigns name of portion of program to ask the que
stions
} //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 obj
ect variables
quizBank[2].answer = "25"; //Initialize object variables
quizBank[3] = new Quiz(); //Creates new object
quizBank[3].question = "Square root of 144"; //Initialize object variab
les
quizBank[3].answer = "12"; //Initialize object variables
quizBank[4] = new Quiz(); //Creates new object
quizBank[4].question = "No. of states in US"; //Initialize object varia
bles
quizBank[4].answer = "50"; //Initialize object variables
quizBank[5] = new Quiz(); //Creates new object
quizBank[5].question = "No. of continents in the world"; //Initialize o
bject 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"; //Init
ialize 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 obj
ect 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 o
bject 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 ke
yboard
System.out.println("********************************"); //prints headin
g top border
System.out.println(" Welcome to my Quiz Application"); //prints heading
System.out.println("********************************"); //prints headin
g 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 use
rs input
{
System.out.println("*** Correct! ***"); //prints correct respon
se
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); //print
s results
System.out.println("*******************"); //prints footer bottom borde
r
} //end of askQuestion

Anda mungkin juga menyukai