Question
Java language
Part IV. Write programs (15 pts each) rite a program that generates a random question of adding or subtracting two integers number and number2 under 20 (numbernumber2 in the case of subtraction) and prompt the user to enter the answer. After the user types the answer in the console, the program displays a message to indicate whether the answer is correct and gives the solution if the answer is wrong. Two sample runs are shown below. Tip: generat question. te a random number (0 or 1) to decide generating an addition question or a subtraction What is 18-9? 7 You are wrong. The answer is 9 What is 12+13? 25 You are right. Good job!
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;
import java.util.Scanner;

public class RandomMathQuestion {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Random random = new Random();
        int n1, n2, result, answer;
        n1 = random.nextInt(20);
        n2 = random.nextInt(20);
        if(random.nextInt(2) == 0) {
            while (n2 > n1) {
                n2 = random.nextInt(20);
            }
            result = n1-n2;
            System.out.print("What is " + n1 + "-" + n2 + "? ");
        } else {
            result = n1+n2;
            System.out.print("What is " + n1 + "+" + n2 + "? ");
        }
        answer = in.nextInt();
        if(answer == result) {
            System.out.println("You are right. Good job!");
        } else {
            System.out.println("You are wrong. The answer is " + result);
        }
    }

}

What is 4-32 You are wrong. The answer is 1 Process finished with exit code O

\color{blue}Hey,\;Please\;let\;me\;know\;if\;you\;need\;me\;to\;make\;any\;changes. \\ I\;would\;really\;appreciate\;an\;upvote.\;Thank\;you!

Add a comment
Know the answer?
Add Answer to:
Java language Part IV. Write programs (15 pts each) rite a program that generates a random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...

    c# prograaming language 1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree. 2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear. 3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree. Write a program that generates 10,000 random integers in the range of 0-9 and store them...

  • Write a C program that generates a random number between 0 and 50. Then, the program...

    Write a C program that generates a random number between 0 and 50. Then, the program prompts for guesses until the user is correct, or has made 10 wrong guesses. After each guess, the program indicates whether the guess was too high, too low, or correct. Once the round has ended, either by a correct guess or by using up the 10 guesses, the program displays the current status.

  • Original Question Problem: Game: learn addition) Write a program that generates two integers under 100 and prompts the...

    Original Question Problem: Game: learn addition) Write a program that generates two integers under 100 and prompts the user to enter the sum of these two integers. The program then reports true if the answer is correct, false otherwise. The program is similar to Listing 4.1. Modification needed to the prgraming code: The following modification nees to be added: the program presents random additions until the user enters -1, or the user answers incorrectly 2 questions in a row. *image...

  • Write a program that generates an array of one hundred random integers between 0 and 9...

    Write a program that generates an array of one hundred random integers between 0 and 9 and displays the count of each number. C programming

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • Write a C++ program that generates a random number from 0 to 9. It will then...

    Write a C++ program that generates a random number from 0 to 9. It will then prompt the user to guess the random number it generated. Your program stops if the user guesses the right number. (Use the cout function to display the random number the computer generates so that you can determine if your code actually works). SAMPLE OUTPUT Random number = 8 Guess a number in the range [0,9]: 1 Too low Guess a number in the range...

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • Write a program that generates an array filled up with random positive integer number ranging from...

    Write a program that generates an array filled up with random positive integer number ranging from 60 to 100, and display it on the screen. After the creation and displaying of the array, the program displays the following: [R]everse [A]dd [S]earch E[xit] Please select an option:_ Then, if the user selects: - R (lowercase or uppercase): the program displays the reversed version of the array. - A (lowercase or uppercase): the program displays the sum of the elements of the...

  • C++ language Write a program that 1. generates a random integer between 100 and 1000, call...

    C++ language Write a program that 1. generates a random integer between 100 and 1000, call the number N 2. reads a file name from the keyboard 3. opens a file for output using the file name from step 2 4. generates N random numbers between 1 and 100 and writes them to the file from step 3 5. closes the file 6. opens the file from steps 3, and 4 for input 7. reads the numbers from the file...

  • Write a JAVASCRIPT program that will help an elementary school student learn multiplication. Use the Random...

    Write a JAVASCRIPT program that will help an elementary school student learn multiplication. Use the Random method to produce two positive one-digit integers. The program should then prompt the user with a question using random numbers, such as How much is 6 times 7? The student then inputs the answer. Next, the program checks the student’s answer. If it is correct, display the message "Very good!" and ask whether the student wants to continue if so then ask another multiplication...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT