I need help with this please Write a program that plays an addition game with th
ID: 3673334 • Letter: I
Question
I need help with this please
Write a program that plays an addition game with the user (imagine the user is a 5th grade student).
First ask the user how many addition problems they want to attempt.
Next, generate two random integers under 100 and prompt the user to enter the sum of these two integers. The program then reports "Correct" or "Incorrect" depending upon the user's "answer". If the answer was incorrect, show the user the the correct sum.
Display a running total of how many correct out of how many total problems ( ex: 2 correct out of 5, 3 correct out of 5, etc)
Continue this until the user has attempted the total number of problems desired.
this is what i have so far. :
import java.util.Scanner;
public class AdditionQuiz {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
System.out.print(
"What is " + number1 + " + " + number2 + "? ");
System.out.println(
number1 + " + " + number2 + " = " + answer + " is " +
( ));
}
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
class AddiotionGame{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random(); // random number generator
int numberOfGame, correct=0, incorrect=0, sum, userSum;
System.out.print("How many attempt do you want: ");
numberOfGame = sc.nextInt();
for(int i=1; i<=numberOfGame; i++){
int random1 = r.nextInt(101); // range is [0 100]
int random2 = r.nextInt(101);
System.out.print("Please try. "+random1+"+"+random2+": ");
userSum = sc.nextInt(); // user answer
if(userSum ==(random1+random2)){
System.out.println("Correct");
correct++;
}else{
System.out.println("Wrong!!. Correct answer is: "+(random1+random2));
incorrect++;
}
}
System.out.println("Correct attempt: "+correct+", Wrong attempt: "+incorrect);
}
}
/*
Output:
How many attempt do you want: 3
Please try. 71+61: 131
Wrong!!. Correct answer is: 132
Please try. 64+22: 86
Correct
Please try. 100+18: 119
Wrong!!. Correct answer is: 118
Correct attempt: 1, Wrong attempt: 2
*/