PROGRAM:
package abc.answers.Programs; import java.util.Scanner; public class GuessNumber { public static void main(String[] args) { // lowest and highest possible numbers int low = 0, high = 100; // boolean variable that represents if we’ve achieved the correct guess boolean correctGuess = false; // variable to store the number of guesses we’ve made int countGuess = 0; // variable to store an initial guess and user input int initialGuess, userInput; // Initializing a Scanner object Scanner input = new Scanner(System.in); // Prompt the user to choose a number System.out.println("Think a number between 0 and 100."); // declaring an array to store all possible guesses int[] arr = new int[101]; // initializing the array with elements for(int i = 0; i <= 100; i++) arr[i] = i; // lower index of array int lower = 0; // upper index of array int upper = 100; // will execute at least once do { // find the middle index of array range between lower and upper int middle = (lower + (upper - lower) / 2); // make initialGuess as the element stored with the middle index initialGuess = arr[middle]; // increment counter countGuess++; // present guess to the user System.out.println("Is " + initialGuess + " the number?"); System.out.println("Enter 1 if this was a correct guess, 2 if your number is higher, and 3 if your number is lower:"); // storing user input userInput = input.nextInt(); // choosing the correct option to proceed if(userInput == 1){ correctGuess = true; System.out.println("Guessed correctly in " + countGuess + " tries!"); } else if(userInput == 2){ // if user number is higher then update the lower limit lower = middle + 1; } else if(userInput == 3){ // if user number is lower then update the upper limit upper = middle - 1; } } // loop till the number is correctly guessed while(correctGuess == false); } }
SCREENSHOTS:
OUTPUT:
Case 1: Guess is 100
Case 2: Guess is 0
Case 3: Guess is 48
CHANGE THE PACKAGE NAME ACCORDINGLY.
HOPE THIS HELPS!
Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...
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 program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...
Computer Guesses - Create a page that allows the user to think of a number from 1 to 100. Write a function that will guess the number the user chose. The user will provide clues: higher or lower and correct. It should take no more than 7 guesses to get the correct number. User Guesses - Create a page that generates a random number from 1 to 100. Allow the user only 7 guesses to guess the number. Provide clues:...
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:...
For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....
JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow of control structures in Java. Specifications For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to...
Write a C++ program using "for" loop to allow user to guess a certain number say 55. The loop will end after 5 guesses or if he guesses the right number, which ever comes first. If user enters a number greater than 55, your program should print "You entered a bigger number". If user enters a number smaller than 55, your program should print "You entered a smaller number". If user enters 55 in less than 5 attempt, your program...
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...
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...
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...