Question

Problem 1: Guess a Number You are writing a program for a number guessing game. For playing Guess a Number, the program wil

USE JAVA

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

In case of any query, do comment. Please rate answer as well. Thanks

Code:

import java.util.Scanner;

public class Main

{

              public static void main(String[] args) {

                  int smallestNumber , largestNumber, number, guess;

                  char symbol;

                             Scanner input = new Scanner(System.in);

                            

                             //take the input from the user for smallest and largest

                             System.out.println("Enter the smallest number to guess:");

                             smallestNumber = input.nextInt();

                            

                             System.out.println("Enter the largest number to guess:");

                             largestNumber = input.nextInt();

                            

                             //generate random number for guessing

                             number = smallestNumber + (int)(Math.random() * ((largestNumber - smallestNumber) + 1));

                            

                             //loop to take guesses from user and act accordingly, assuming the format of guess would be same as given symbol then space and then value

                             while(true){

                               

                             System.out.println("Enter your guess (sign + number)");

                             symbol = input.next().charAt(0);

                            

                             guess = input.nextInt();

                            

                             if(symbol == '>'){

                                 if(number > guess )

                                     System.out.println("Yes");

                                 else

                                     System.out.println("No");

                             }

                             else if(symbol == '<'){

                                   if(number < guess )

                                     System.out.println("Yes");

                                 else

                                     System.out.println("No");

                             }

                             else if(symbol == '=')

                             {

                                 if(number == guess){

                                     System.out.println("Correct, you win!!");

                                 }

                                 else

                                     System.out.println("Incorrect guess, the number was " + number + ", you lose!");

                                     

                                 break;

                             }

                            

                             }

                            

              }

}

===============screens hot of the code=========

Main.java 1 import java.util.Scanner; 3 public class Main public static void main(String[] args) { int smallest Number , larg

Output:

input Enter the smallest number to guess: Enter the largest number to guess: Enter your guess (sign + number) > 12 Yes Enter

Enter the smallest number to guess: Enter the largest number to guess: 20 Enter your guess (sign + number) > 10 Yes Enter you

Add a comment
Know the answer?
Add Answer to:
USE JAVA Problem 1: Guess a Number You are writing a program for a number guessing...
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
  • 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...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

  • Swift program: Develop an app that is a Number Guessing Game, (ex: one that allows a...

    Swift program: Develop an app that is a Number Guessing Game, (ex: one that allows a player to guess a number between a high and a low number, say between 1 and 10) with the system guiding the player by indicating whether the number is too high or too low after an incorrect guess. This process is repeated until the player correctly guesses the number or simply quit your app. For simplicity purposes, assume the magic number to be guessed...

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

  • Write a Java program that implements the number guessing game where the computer tries to guess...

    Write a Java program that implements the number guessing game where the computer tries to guess a number in your head. You pick a number between 0 and 100, and the computer generates a guess. You then type 1 if your number is larger than the computer’s guess, -1 if your number is smaller, and 0 if the number is correct. The computer should keep trying to guess the number by adjusting the guess based on your feedback (so if...

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

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

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

  • Use C++. In this project you will be tasked with writing a program to play a...

    Use C++. In this project you will be tasked with writing a program to play a guessing game between another player and your program. Your program will start by initializing an array to 5 fixed values in the ranges of 1-1000 in the array. These values should also be in order (sorted). Your program will then begin by asking the player to make a guess from 1-1000. For the first guess of the user, your program will only tell the...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

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