Write a class called QuizProgram that simulates a quiz that helps somebody pract
ID: 3747668 • Letter: W
Question
Write a class called QuizProgram that simulates a quiz that helps somebody practice addition and subtraction. The program must randomly compute 5 quiz questions which are either addition or subtraction questions in the form of N1 - N2_or N1 N2- types in the guessed answer to the math expression. N1 should be a number from 0 to 99 and N2 should be a number from 0 to N1. The decision to use an addition or subtraction operator in the question should be chosen randomly as well, with equal probability. If the user types in the correct answer, the program should say "You are correct!", otherwise it should print "Sorry, the correct answer is-'. After the 5 questions have been asked, the program should show the % of correct answers. Here is example output (highlighted text indicates user input) where the user What is the answer to 3 0 3 You are correct! What is the answer to 22- 11 11 You are correct! What is the answer to 17-13-4 You are correct! What is the answer to 93 21 5 Sorry, the correct answer is 114 What is the answer to 7012 82 You are correct!Explanation / Answer
AddSubQuiz.java
import java.util.Random;
import java.util.Scanner;
public class AddSubQuiz {
public static void main(String[] args) {
int num, N1, N2, ans, flag = 0, cAns = 0, cnt = 0;
//Creating a random Class object
Random r = new Random();
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 5; i++) {
num = r.nextInt(2) + 1;
flag = 0;
if (num == 1) {
N1 = r.nextInt((99) + 1);
N2 = r.nextInt((N1) + 1);
System.out.print("What is the answer to " + N1 + " + " + N2 + " = ");
ans = sc.nextInt();
cAns = N1 + N2;
if (cAns == ans) {
flag = 1;
cnt++;
}
} else {
N1 = r.nextInt((99) + 1);
N2 = r.nextInt((N1) + 1);
System.out.print("What is the answer to " + N1 + " - " + N2 + " = ");
ans = sc.nextInt();
cAns = N1 - N2;
if (cAns == ans) {
flag = 1;
cnt++;
}
}
if (flag == 1) {
System.out.println("You are correct!");
} else {
System.out.println("Sorry, the correct answer is " + cAns);
}
}
System.out.println("You scored " + ((cnt / 5.0) * 100) + " % on the quiz");
}
}
________________
Output:
What is the answer to 47 + 17 = 64
You are correct!
What is the answer to 58 + 11 = 69
You are correct!
What is the answer to 76 + 48 = 124
You are correct!
What is the answer to 14 + 11 = 23
Sorry, the correct answer is 25
What is the answer to 81 + 58 = 139
You are correct!
You scored 80.0 % on the quiz
_______________Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.