Question

In: Computer Science

Write a program that plays an addition game with the user (imagine the user is a...

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 in the range between 10 and 50 inclusive. You must use the Math.random( ) method call.   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) after each problem is answered.

Continue this until the user has attempted the total number of problems desired.

java

Solutions

Expert Solution

JAVA PROGRAM:

import java.util.*;

// class Test

class Test {

    // main() nethod

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        // Declare the variable

        // Random integers in the range between 10 and 50 inclusive

        int low = 10;

        int up = 50;

        int userAns,correctAns;

        int correct = 0;

        // Ask the user how many addition problems they want

        System.out.print("How many addition problems they want to attempt: ");

        int n = sc.nextInt();

        // Continue this until the user has attempted the total number of problems desired.

        for(int i=0;i<n;i++){

                // Generate two random integers num1 and num2

                // using Math.random() method call

                int num1 = (int)(Math.random() * ((up - low)+1)) + low;

                int num2 = (int)(Math.random() * ((up - low)+1)) + low;

                // calculate the correct sum

                correctAns = num1 + num2;

                // display the two random number

                System.out.print("Sum is "+num1+" + "+num2+" = ");

                // ask the student enter the sum of two number

                userAns = sc.nextInt();

                // if the user answer is correct

                if(userAns==correctAns){

                    // then display the correct

                    System.out.println("Correct");

                    // count the correct answer

                    correct++;

                }else{

                    // if the user answer is wrong then print the incorrect messgae

                    System.out.println("Incorrect");

                    // Display the two random number and their sum

                    System.out.println("correct is "+num1+" + "+num2+" = "+correctAns);

                }

                // Display the how many correct out of how many total problems

                System.out.println(correct+" correct out of "+n);

                System.out.println();     

        }

    }

}

OUTPUT:

NOTE: If you don't understand any step please tell me.


Related Solutions

Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
1. Write a program that plays a simple dice game between the computer and the user....
1. Write a program that plays a simple dice game between the computer and the user. When the program runs, it asks the user to input an even number in the interval [2..12], the number of plays. Display a prompt and read the input into a variable called ‘plays’ using input validation. Your code must ensure that the input always ends up being valid, i.e. even and in [2..12]. 2. A loop then repeats for ‘plays’ iterations. Do the following...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
For this problem, you will write a program in which the computer plays a dice game...
For this problem, you will write a program in which the computer plays a dice game called Craps. You might need to explore some Python documentation on random() for this homework assignment. PROVIDE A GRAPHICAL FLOWCHART as part of your submission for this solution Rules: The player rolls two six-sided dice. After rolling, the sum of what the dice show is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
Using Python, Assignment Write a program that plays a game of craps. The program should allow...
Using Python, Assignment Write a program that plays a game of craps. The program should allow the player to make a wager before each “turn”. Before each turn, allow the user to either place a bet or exit the game. After each turn display the player’s current balance. Specifics Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program. Implement this any way you wish, but make it...
Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT