Question

Write a JAVA program that plays a number guessing game with the user. A sample run...

Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run.

Welcome to the game of Guess It!
I will choose a number between 1 and 100.
You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low.
You have 6 tries to get the number.

OK, I am thinking of a number. Try to guess it.

Your guess? 50
Too high!
Your guess? 112
Illegal guess. Your guess must be between 1 and 100. Try again. Your guess? 23

**** CORRECT ****

Want to play again? N
Good bye, it was fun. Play again soon.

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

package com;

import java.util.Random;

import java.util.Scanner;

public class GuessIt {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Welcome to GuessIt");

Random rand=new Random(); // Create a Random object

// System generating a random number between 1 to 100

int score=rand.nextInt(99)+1; // rand.nextInt((max-min)+1) + min  

// Player Guess

Scanner sc=new Scanner(System.in);

int count=0;

do { // Do wjile loop which will ensure to run up to 6 user inputs

System.out.println("Please guess the number");

int guess=sc.nextInt();

if(guess>100) // Try again if guessed number is greater than 100

{

System.out.println("Ilegal guess, Try entering between 1 and 100");

}

else if((guess-score)>0) // guessed number is greater than score

{

if(guess-score>10) // If the difference is greater than 10, it is too higher

{

System.out.println("Guessed number is too higher");

}

else // If the difference is lesser than 10, it is just higher

{

System.out.println("Guessed number is higher");

}

}

else if(guess-score<0) // guessed number is lower than score

{

if(guess-score<-10) // If the difference is lower than -10, it is too lower

{

System.out.println("Guessed number is too lower");

}

else // If the difference is greater than -10, it is just lower

{

System.out.println("Guessed number is lower");

}

}

else if(guess==score) // Correct guess

{

System.out.println(" *******CORRECT****** ");

}

else

{

System.out.println("Invalid entry");

}

count++; // Increment the count

}while(count!=6); // Check inputs until equal to 6

}

}

Please upvote if you like the answer and comment if you need any further information in this regard.

Add a comment
Know the answer?
Add Answer to:
Write a JAVA program that plays a number guessing game with the user. A sample run...
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
  • 7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number”...

    7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number” as follows: Your app chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The app displays the prompt "Guess a number between 1 and 1000: ". The player inputs a first guess. If the player’s guess is incorrect, your app should display Too high. Try again. or Too low. Try again. to help the player “zero...

  • 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...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • python Exercise: ex9.py Write a program that plays a number and you guess the number. The...

    python Exercise: ex9.py Write a program that plays a number and you guess the number. The program helps you with a response 1t the number you guessed is higher or lower than the mystery number. To generate a random number, we need to import some routines. guessing game. It generates a random mystery import random mysteryNumber random.randint (1, 100) The random.randint (1, 100) creates a random integer between 1 and 100. Here is a sample output: Let's play the guessing...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • A Backwards Guessing Game For this assignment, you will be a making a guessing game with...

    A Backwards Guessing Game For this assignment, you will be a making a guessing game with a twist. You will play the role of the computer, and the computer will try to guess the number. You will think of a number (you don’t have to read it in as input) between 1 and 100. You will create a loop that runs 5 times. In this loop, the computer will try to guess the number in the range it knows is...

  • Write a C++ program to play the number guessing game with user as follows.

    Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

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