For this lab you will write a Java program that will run a simple math quiz. You
ID: 3549544 • Letter: F
Question
For this lab you will write a Java program that will run a simple math quiz. Your program will generate two random integers between 1 and 20 and then ask a series of math questions. Each question will be evaluated as to whether it is the right or wrong answer. In the end a final score should be reported for the user.
This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.
Enter your name: Jeremy
Welcome Jeremy! Please answer the following questions:
4 + 6 = 10
Correct!
4 * 6 = 24
Correct!
4 / 6 = 1
Wrong!
The correct answer is 0
4 % 6 = 4
Correct!
You got 3 correct answers
That's 75.0%!
Your code will behave differently based on the random numbers it selects and the answers provided by the user. Here is a second possible execution of this code:
Enter your name: Bob
Welcome Bob! Please answer the following questions:
3 + 3 = 0
Wrong!
The correct answer is 6
3 * 3 = 6
Wrong!
The correct answer is 9
3 / 3 = 0
Wrong!
The correct answer is 1
3 % 3 = 1
Wrong!
The correct answer is 0
You got 0 correct answers
That's 0.0%!
Explanation / Answer
package com.chegg;
import java.util.Random;
import java.util.Scanner;
public class LabQuiz {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("****Welcome to Quiz******");
System.out.print("Enter your name: ");
String name = in.next();
System.out.println("Welcome "+name+"! Please answer the following questions:");
int scoreCounter=0;
int a;
int b;
int response;
a=getRandomNum();
b=getRandomNum();
System.out.print(a+"+"+b+"=");
response = in.nextInt();
if(response==(a+b)){
scoreCounter++;
System.out.println("That is correct");
}
else
System.out.println("No, thats not the right answer, its::"+(a+b));
a=getRandomNum();
b=getRandomNum();
System.out.print(a+"*"+b+"=");
response = in.nextInt();
if(response==(a*b)){
scoreCounter++;
System.out.println("That is correct");
}
else
System.out.println("No, thats not the right answer, its::"+(a*b));
a=getRandomNum();
b=getRandomNum();
System.out.print(a+"/"+b+"=");
response = in.nextInt();
if(response==(a/b)){
scoreCounter++;
System.out.println("That is correct");
}
else
System.out.println("No, thats not the right answer, its::"+(a/b));
a=getRandomNum();
b=getRandomNum();
System.out.print(a+"%"+b+"=");
response = in.nextInt();
if(response==(a%b)){
scoreCounter++;
System.out.println("That is correct");
}
else
System.out.println("No, thats not the right answer, its::"+(a*b));
System.out.println("You got "+scoreCounter+" correct answers.");
System.out.println("Thats "+(scoreCounter*25)+"%");
in.close();
}
static int getRandomNum(){
Random rand = new Random();
int a;
a = rand.nextInt(20);
if(a==0)
a++;
return a;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.