Math Quiz in Java

Often times, computer programs are used as a study method by educators. In this program, many of the concepts we have gone over thus far are put together to make a working quiz that asks math questions, and returns results on how the user did. For this program, the user is asked how many math questions they would like. It generates random math questions with numbers 1 through 10, and randomly selects between addition, subtraction, multiplication. and division. If the user is right, it will tell them so. If they are wrong, they will be given the correct answer.
  1. import java.util.*;
  2.  
  3. public class math
  4. {
  5.  
  6.         public static Scanner keyboard = new Scanner(System.in); //declares public scanner object for use in all methods
  7.        
  8.         public static void main(String[] args) //main method which is mainly used for prompt and calling other methods
  9.         {
  10.        
  11.                 System.out.print("How many math questions do you want: ");
  12.                 int j = keyboard.nextInt();
  13.  
  14.                 keyboard.nextLine(); //clears buffer (needed for use of scanner again)
  15.                
  16.                 if (j == 0) //conditional checking if j is 0
  17.                 {
  18.                     System.out.println("That's no fun!\n");
  19.                     System.exit(0); //closes program
  20.                  }
  21.                    
  22.                 if (j < 0) //this conditional is for if the user enters a negative value
  23.                     j = j * -1; //converts negative input to positive to avoid errors
  24.                
  25.                 int i = 0; //initializes counter variable, i
  26.                 while (i < j) //loop that will run as long as counter is lower than the number input by user
  27.                 {
  28.                     randy(); //calls randy method
  29.                     i++; //increments i by 1
  30.                 }
  31.                
  32.                 finaloutput(); //calls finaloutput method after loop finishes
  33.                
  34.         }
  35.        
  36.        
  37.         public static void randy() //randy method is used for creating random numbers and passing them to calc method
  38.         {
  39.                 Random randy1 = new Random(); //declares randy1 as a new random variable
  40.                 Random randy2 = new Random(); //declares randy2
  41.                 int m = randy1.nextInt(10)+1; //makes m equal to a random number between 1 and 10
  42.                 int n = randy1.nextInt(10)+1; //does the same for n
  43.                 int o = randy2.nextInt(3); //makes o equal to a random number between 0 and 2 for deciding an operation
  44.  
  45.                 calc(m,n,o); //passes the three random values to calc method
  46.         }
  47.        
  48.         public static int numright = 0; //public integers for keeping track of score
  49.         public static int numwrong = 0;
  50.        
  51.         public static void calc(int a, int b, int c) //accepts values passed to it from randy method
  52.         {
  53.                
  54.                 double answer = 0; //declares answer variable
  55.                
  56.                 if (c == 0) //conditional checking if third random number generated is 0
  57.                 {
  58.                     answer = a + b; //answer to upcoming problem
  59.                     System.out.print("What is " + a + " + " + b + ": ");
  60.                 }
  61.                
  62.                 if (c == 1) //conditional if random number is 1
  63.                 {
  64.                     answer = a - b; //answer is stored in memory
  65.                     System.out.print("What is " + a + " - " + b + ": "); //gives subtraction problem
  66.                 }
  67.                
  68.                 if (c == 2) //if random number is 2
  69.                 {
  70.                     answer = a * b; //answer stored in memory
  71.                     System.out.print("What is " + a + " x " + b + ": "); //multiplication problem
  72.                 }
  73.                
  74.                
  75.                 double input = keyboard.nextDouble();
  76.                
  77.                 if (input == answer) //if user's answer is correct
  78.                     {
  79.                     System.out.println("That is correct!\n");
  80.                     numright++; //ups count of numright
  81.                     }
  82.                    
  83.                 else //if user is wrong
  84.                     {
  85.                     System.out.println("That is incorrect! The answer is " + answer + "\n");
  86.                     numwrong++; //ups count of numwrong
  87.                     }
  88.                        
  89.         }
  90.        
  91.         public static void finaloutput()
  92.         {
  93.                 double percentage = numright*100 / (numright + numwrong); //finds the percentage of questions answered correctly
  94.                
  95.                 System.out.println("The number of questions you answered correctly is " + numright + " and the number you answered incorrectly is " + numwrong + ". The percentage of questions answered correctly is " + percentage + "%.\n");
  96.         }
  97. }
Be sure to take note of comments, as they explain the code as it goes along. Three random numbers are generated for each question. The first two are any numbers 1-10, and the second is any number 0-2 for determining whether it will deal with addition, subtraction, or multiplication. The end of the calc method keeps tally on how many the user got right, and how many they got wrong. When the quiz is finished, the user is told how many they got right and wrong, and gives them a percentage/score.

Add new comment