Question

In: Computer Science

JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...

JAVA PROGRAM

Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many guesses that were correct and how many that were wrong. Afterthe five subtractions done, display the number of correct and wrong answers.

Solutions

Expert Solution

Explanation:

Here is the code which creates 2 random numbers and then takes the guess for the subtraction from the user.

Then, it happens again and again for new numbers generated everytime and correct and wrong answers are displayed at the end.

Code:

import java.util.Random;
import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       Random r = new Random();
       Scanner sc = new Scanner(System.in);
      
       int correct = 0, wrong = 0;
      
       for(int i=0; i<5; i++)
       {
       int a = r.nextInt(10) + 1;
       int b = r.nextInt(10) + 1;
      
       System.out.println("Random Numbers generated!");
       if(b>a)
       {
       int temp = a;
       a = b;
       b = temp;
       }
      
       System.out.print("Enter guess for subtraction: ");
       int guess = sc.nextInt();
      
       if(guess == (a-b))
       {
       System.out.println("Correct");
       correct++;
       }
       else
       {
       System.out.println("Wrong");
       wrong++;
       }
       }
       System.out.println("Correct answers: "+correct);
       System.out.println("Wrong answers: "+wrong);
      
   }
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Write a java program that inserts 25 random integers from 0 to 100 in order into...
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT