Question

Write a program that prompts the user to enter a number within the range of 1...

Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive.

The program then generates a random number using the random class: If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number, If the users guess matches the random number tell them they win! If the users guess does not match the random number tell them they are wrong.

Use a loop (WHILE or DO) to allow the program to be repeated by typing in the word “YES” ignore case sensitivity.

Ensure that every time you repeat a NEW random number is generated.

Increment the appropriate variables upon the following instances: A win (add 1 to winCount), A loss (add 1 to lossCount), New game played (add 1 to playCount)

At the end of the program neatly display the user their stats (Their times played, their win count and their losses)

Thank you kindly for your time and help, it is greatly appreciated!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is a code done in Java for the above problem along with the output and explained with comments

CODE:

import java.util.Random;// importing the random class
import java.util.Scanner;// importing the scanner class

public class RandomNumberGame{
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);//instantiating the scanner class
       Random random = new Random();//instantiating the random class
       int playCount = 0;// the number of plays
       int lossCount = 0;// the number of wins
       int winCount = 0;// the number of losses

       int randomNumber;// the generated random number
       int guessedNumber;// the number that the user guesses

       String wantToPlay = "YES";// the string variable that holds users wish to play on
       // continue while the answer is yes
       while((wantToPlay.equals("YES")) || (wantToPlay.equals("yes"))){
           playCount++;// increment the number of plays
           System.out.println("Enter a number in range 1 to 10(both inclusive):");
           guessedNumber = scan.nextInt();// getting the guessed number from user

           randomNumber = random.nextInt((10 - 1) + 1) + 1;// generating random number within 1-10
           // if the numbers are same
           if(guessedNumber == randomNumber){
               winCount++;//increment the user's winCount
               System.out.println("You win!!!");//message
           }
           else{
               lossCount++;//else increment the user's lossCount
               System.out.println("Sorry..You are wrong!!!");// a message
           }
          
           scan.nextLine();
           System.out.println("Want to play again?");//prompt to play again
           wantToPlay = scan.nextLine();//taking the input into wantToPlay


       }
       // Displaying the stats at the end of the game
       System.out.println("Your play Stats:");
       System.out.println("Total games played: " + playCount);
       System.out.println("Number of wins:" + winCount);
       System.out.println("Number of losses:" + lossCount);
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user to enter a number within the range of 1...
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
  • Python Code Write a program using functions and mainline logic which prompts the user to enter...

    Python Code Write a program using functions and mainline logic which prompts the user to enter a number. The number must be at least 5 and at most 20. (In other words, between 5 and 20, inclusive.) The program then generates that number of random integers and stores them in a list. The random integers should range from 0 to 100. (You can use a wider range if you want, but the lower end of the range must be at...

  • Write a computer program that prompts the user for one number, n for the number of...

    Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of the loop, Create an array of n random integers (Make sure...

  • Write a loop statement that prompts the user to guess a secret number. Set the secret...

    Write a loop statement that prompts the user to guess a secret number. Set the secret number before the loop. Continue executing until the user correctly guesses the secret number. If they come within 10 numbers either above or below tell the user they are getting warmer. If they are outside of 10 numbers tell them they are getting colder.(Python)

  • python program 6 Write an input validation loop that asks the user to enter a number...

    python program 6 Write an input validation loop that asks the user to enter a number in the range of 100 through 1000? 7 Write an input validation loop that asks the user to enter ‘Y’, ‘y’, ‘N’, or ‘n’? 8 How many times the following loop will repeat? cnt = 0    while  cnt != 5: print(cnt) cnt = cnt + 2

  • Java: Write a program that prompts the user to enter integers in the range 1 to...

    Java: Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be: 10 occurs 4 times 20 occurs 2 times 25 occurs 1 time 30 occurs 1 time...

  • Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...

    Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as "You win!" and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Write a code segment that prompts the user to enter a double number between (5.55 and...

    Write a code segment that prompts the user to enter a double number between (5.55 and 123.3) inclusive. If the input is invalid, print an error message and loop until the input is valid. Code in c++

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • Design a Python program that prompts the user to enter "yes" or "no" and validates the...

    Design a Python program that prompts the user to enter "yes" or "no" and validates the input. (Use a case-insensitive comparison) and another Python program that prompts the user to enter a number in the range of 1 through 100 and validates the input

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