Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a function called mathgame.n that repeatedly asks a user to answer a si

ID: 3726441 • Letter: 2

Question

2. Write a function called mathgame.n that repeatedly asks a user to answer a simple addition the user is told if their answer is correct or incorrect and they question. After each e are given the opportunity to continue or to stop. When they stop a summary of their results for the game is provided. Tackle this problem in small pieces. The following is a suggested strategy but certainly not the only one: . Create two random integers between 1 and 10 and store them in separate variables. . Compute the sum of the two integers and store the result in another variable. ·Output the questio so the user can see the two random mmhers but can not see the result of the sum Prompt the user to enter their guess and collect it using the input function. numbers. If they match print out 'correct'. If they don't print out incorrect. is initially set to true or your program won't run. . Determine if the user's guess matches the result that you have stored for the sum of the . If the above steps are working, wrap them in a while loop. Make sure you loop condition #3a Next page. Page 2 of7 COSC 2836 - Winter 18 Assignment #2 . The lust line of your loop shoould be to ask the user if they want another question. If they enter 'y the loop repeats. Anything else and the loop ends. Finally, use variables to keep track of the number of questions, the number correct and the number incorrect. Display this information as shown in the output below onathgane) 1: 3 4-7 Correct! Again? n Correct: 1 (100.00%). tncorrect 0 (0.00%» >> nathgame

Explanation / Answer

import java.util.Random;

import java.util.Scanner;

public class MathGame {

public static void main(String a[]) {

mathGame();

}

/**

* math Game

*/

public static void mathGame() {

int questionCount = 0;

int correctAnswerCount = 0;

int wrongAnswerCount = 0;

Scanner sc = new Scanner(System.in);

try {

for (;;) {

System.out.println(">>maathgame");

System.out.println("Welcome to the math game !");

questionCount++;

int firstNumber = getRandomNumber();

int secondNuber = getRandomNumber();

int result = firstNumber + secondNuber;

System.out.println(questionCount + " : " + firstNumber + " + " + secondNuber + " = ");

int userAnswer = sc.nextInt();

if (userAnswer == result) {

System.out.println("Correct!");

correctAnswerCount++;

} else {

System.out.println("Incorrect!");

wrongAnswerCount++;

}

System.out.print("Again(y/n)? ");

String userResponse = sc.next();

if (!userResponse.equals("y")) {

System.out.println("Correct: " + correctAnswerCount + " ("

+ (correctAnswerCount * 100 / questionCount) + "%)," + " incorrect: " + wrongAnswerCount

+ " (" + (wrongAnswerCount * 100 / questionCount) + "%)");

break;

}

}

} catch (Exception e) {

System.out.println("Entered input is wrong. So closing the game");

System.out

.println("Correct: " + correctAnswerCount + " (" + (correctAnswerCount * 100 / questionCount)

+ "%)," + " incorrect: " + wrongAnswerCount + " ("

+ (wrongAnswerCount * 100 / questionCount) + "%)");

}

}

/**

* generate random number between 1-10

*

* @return

*/

private static int getRandomNumber() {

Random r = new Random();

return r.nextInt((10 - 1) + 1) + 1;

}

}

sample output >>>

>>maathgame
Welcome to the math game !
1 : 4 + 6 =
10
Correct!
Again(y/n)? y
>>maathgame
Welcome to the math game !
2 : 1 + 1 =
2
Correct!
Again(y/n)? y
>>maathgame
Welcome to the math game !
3 : 6 + 8 =
9
Incorrect!
Again(y/n)? n
Correct: 2 (66%) incorrect: 1 (33%)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote