Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Language: Use the file made available under this exercise, in this module, to complete this task. The program prompts a user to enter a

Java Language:

Usethefile made available under this exercise, in this module, to complete this task. The program prompts a user to enter a choice between 1-5. 1-4 correspond to addition, subtraction, multiplication, and division respectively. Depending on the users choice, the program generates two random numbers and asks the user a simple math question using the arithmetic operator corresponding to the users choice. For example, if the user enter 2 (subtraction), the user might be askedwhat is 10-2?If the user answer is correct, the program displaysCorrect, otherwise, the program displaysYour answer is wrong. The correct answer is blah.Then, then user is prompted to enter another choice. This is how the program will continue until the user enters 5 to exit the program. You can execute the program and test it to see the way it works. Then,use commentto explain what each line of the code does. Your explanation must be clear enough to show that you understand the code

class SimpleMathGame{ public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); while (true) { System.out.println(\"Main menu\"); System.out.println(\"1: Addition\"); System.out.println(\"2: Subtraction\"); System.out.println(\"3: Multiplication\"); System.out.println(\"4: Division\"); System.out.println(\"5: Exit\"); System.out.print(\"Enter a choice: \"); int choice = input.nextInt(); int number1 = (int)(Math.random() * 10); int number2 = (int)(Math.random() * 10); int answer; if (choice == 1) { System.out.print(\"What is \" + number1 + \" + \" + number2 + \"? \"); answer = input.nextInt(); if (number1 + number2 == answer) System.out.println(\"Correct\"); else System.out.println(\"Your answer is wrong. The correct answer is \" + (number1 + number2)); } else if (choice == 2) { if (number1 int temp = number1; number1 = number2; number2 = temp; } System.out.print(\"What is \" + number1 + \" - \" + number2 + \"? \"); answer = input.nextInt(); if (number1 - number2 == answer) System.out.println(\"Correct\"); else System.out.println(\"Your answer is wrong. The correct answer is \" + (number1 - number2)); } else if (choice == 3) { System.out.print(\"What is \" + number1 + \" * \" + number2 + \"? \"); answer = input.nextInt(); if (number1 * number2 == answer) System.out.println(\"Correct\"); else System.out.println(\"Your answer is wrong. The correct answer is \" + (number1 * number2)); } else if (choice == 4) { while (number2 == 0) number2 = (int)(Math.random() * 10); System.out.println(\"What is \" + number1 + \" / \" + number2 + \"? \"); answer = input.nextInt(); if (number1 / number2 == answer) System.out.println(\"Correct\");

else System.out.println(\"Your answer is wrong. The correct answer is \" + (number1 / number2)); } else if (choice == 5) break; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Android Graphics

Authors: Wallace Jackson

1st Edition

1430257857, 978-1430257851

More Books

Students also viewed these Programming questions