Math Quiz in Java
Submitted by GeePee on Sunday, May 10, 2015 - 23:37.
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.
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.
- import java.util.*;
- public class math
- {
- public static Scanner keyboard = new Scanner(System.in); //declares public scanner object for use in all methods
- public static void main(String[] args) //main method which is mainly used for prompt and calling other methods
- {
- int j = keyboard.nextInt();
- keyboard.nextLine(); //clears buffer (needed for use of scanner again)
- if (j == 0) //conditional checking if j is 0
- {
- }
- if (j < 0) //this conditional is for if the user enters a negative value
- j = j * -1; //converts negative input to positive to avoid errors
- int i = 0; //initializes counter variable, i
- while (i < j) //loop that will run as long as counter is lower than the number input by user
- {
- randy(); //calls randy method
- i++; //increments i by 1
- }
- finaloutput(); //calls finaloutput method after loop finishes
- }
- public static void randy() //randy method is used for creating random numbers and passing them to calc method
- {
- int m = randy1.nextInt(10)+1; //makes m equal to a random number between 1 and 10
- int n = randy1.nextInt(10)+1; //does the same for n
- int o = randy2.nextInt(3); //makes o equal to a random number between 0 and 2 for deciding an operation
- calc(m,n,o); //passes the three random values to calc method
- }
- public static int numright = 0; //public integers for keeping track of score
- public static int numwrong = 0;
- public static void calc(int a, int b, int c) //accepts values passed to it from randy method
- {
- double answer = 0; //declares answer variable
- if (c == 0) //conditional checking if third random number generated is 0
- {
- answer = a + b; //answer to upcoming problem
- }
- if (c == 1) //conditional if random number is 1
- {
- answer = a - b; //answer is stored in memory
- }
- if (c == 2) //if random number is 2
- {
- answer = a * b; //answer stored in memory
- }
- double input = keyboard.nextDouble();
- if (input == answer) //if user's answer is correct
- {
- numright++; //ups count of numright
- }
- else //if user is wrong
- {
- numwrong++; //ups count of numwrong
- }
- }
- public static void finaloutput()
- {
- double percentage = numright*100 / (numright + numwrong); //finds the percentage of questions answered correctly
- 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");
- }
- }
Add new comment
- 2109 views