Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import the Scanner, Random, and ArrayList classes
- import java.util.*;
- public class javaQuiz
- {
- // Declare variable for the question that the user is going to answer at any given time, and the variable for the user's answer.
- public static String chosenQuestion;
- public static char userChoice;
- // Declare variables for number of correct and incorrect answers.
- public static int correctTally = 0;
- public static int incorrectTally = 0;
- public static void main (String [] args)
- {
- // creating a new Scanner object
- Scanner keyboard = new Scanner(System.in);
- // Give the user a readable description of the program
- System.out.println("In this program you will be answering 3 multiple choice questions about Java.");
- System.out.println("Press any key to proceed.");
- String pressAnyKey = keyboard.nextLine();
- // Create the Question objects.
- javaQuizQuestion question1 = new javaQuizQuestion("Which of these is not a Java key word?\n", "\nA) switch\n", "\nB) keyboard\n", "\nC) float\n", "\nD) true\n", "\nB) keyboard\n");
- javaQuizQuestion question2 = new javaQuizQuestion("What word must a switch-case statement end with?\n", "\nA) switch\n", "\nB) break\n", "\nC) continue\n", "\nD) else\n", "\nB) break\n");
- javaQuizQuestion question3 = new javaQuizQuestion("Which of these is not a primitive data type?\n", "\nA) string\n", "\nB) boolean\n", "\nC) long\n", "\nD) char\n", "\nA) string\n");
- // Create an ArrayList which will store the Question objects
- ArrayList<P3A2_WONDER_RJW84_Question> questionList = new ArrayList<P3A2_WONDER_RJW84_Question>();
- // Add the Questions to the arrayList.
- questionList.add(question1);
- questionList.add(question2);
- questionList.add(question3);
- // randomize the order of the questions in the ArrayList
- Collections.shuffle(questionList);
- // Play the game with the user using a for-loop that will stop once there are no more questions.
- for (int index = 0; index < questionList.size(); index++)
- {
- System.out.print(questionList.get(index).toString());
- // Call the method that will get the user's answer.
- getAnswer();
- // Determine if the user answered correctly or not.
- if (userChoice == correctAns)
- {
- System.out.println("Correct!");
- correctTally+= 1;
- System.out.println("Your current number of correct answers is: " + correctTally + " and incorrect answers: " + incorrectTally);
- }
- else
- {
- System.out.println("Incorrect.");
- correctTally-= 1;
- System.out.println("Your current number of correct answers is: " + correctTally + " and incorrect answers: " + incorrectTally);
- }
- }
- }
- // method that will retrieve the answer from the user
- public static char getAnswer()
- {
- // create another Scanner object
- Scanner keyboard = new Scanner(System.in);
- // Tell the user to select their answer and store it in a variable.
- System.out.println("\nSelect your answer by pressing A, B, C, or D.");
- String input = keyboard.nextLine();
- userChoice = input.charAt(0);
- // Return the user's answer to main.
- return userChoice;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement