Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. // Import the Scanner, Random, and ArrayList classes
  2. import java.util.*;
  3.  
  4. public class javaQuiz
  5. {
  6. // Declare variable for the question that the user is going to answer at any given time, and the variable for the user's answer.
  7. public static String chosenQuestion;
  8. public static char userChoice;
  9.  
  10. // Declare variables for number of correct and incorrect answers.
  11. public static int correctTally = 0;
  12. public static int incorrectTally = 0;
  13.  
  14. public static void main (String [] args)
  15. {
  16. // creating a new Scanner object
  17. Scanner keyboard = new Scanner(System.in);
  18.  
  19. // Give the user a readable description of the program
  20. System.out.println("In this program you will be answering 3 multiple choice questions about Java.");
  21. System.out.println("Press any key to proceed.");
  22. String pressAnyKey = keyboard.nextLine();
  23.  
  24. // Create the Question objects.
  25. 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");
  26. 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");
  27. 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");
  28.  
  29. // Create an ArrayList which will store the Question objects
  30. ArrayList<P3A2_WONDER_RJW84_Question> questionList = new ArrayList<P3A2_WONDER_RJW84_Question>();
  31.  
  32. // Add the Questions to the arrayList.
  33. questionList.add(question1);
  34. questionList.add(question2);
  35. questionList.add(question3);
  36.  
  37. // randomize the order of the questions in the ArrayList
  38. Collections.shuffle(questionList);
  39.  
  40. // Play the game with the user using a for-loop that will stop once there are no more questions.
  41. for (int index = 0; index < questionList.size(); index++)
  42. {
  43. System.out.print(questionList.get(index).toString());
  44.  
  45. // Call the method that will get the user's answer.
  46. getAnswer();
  47.  
  48. // Determine if the user answered correctly or not.
  49. if (userChoice == correctAns)
  50. {
  51. System.out.println("Correct!");
  52. correctTally+= 1;
  53. System.out.println("Your current number of correct answers is: " + correctTally + " and incorrect answers: " + incorrectTally);
  54. }
  55. else
  56. {
  57. System.out.println("Incorrect.");
  58. correctTally-= 1;
  59. System.out.println("Your current number of correct answers is: " + correctTally + " and incorrect answers: " + incorrectTally);
  60. }
  61. }
  62. }
  63.  
  64. // method that will retrieve the answer from the user
  65. public static char getAnswer()
  66. {
  67. // create another Scanner object
  68. Scanner keyboard = new Scanner(System.in);
  69.  
  70. // Tell the user to select their answer and store it in a variable.
  71. System.out.println("\nSelect your answer by pressing A, B, C, or D.");
  72. String input = keyboard.nextLine();
  73. userChoice = input.charAt(0);
  74.  
  75. // Return the user's answer to main.
  76. return userChoice;
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement