Write a program that will guess a number that the user thinks up based on the ru
ID: 3760216 • Letter: W
Question
Write a program that will guess a number that the user thinks up based on the rules below: 1. You must ask the user to think of an integer between 1 and 1000. They do not type this number into the computer. They just think of it in their head. 2. The computer will make an initial guess of the number. 3. The user must indicate if the guess is too high, too low or correct (use whatever input format you want for this, but specify it in the comments and prompt, as usual). 4. If the computer has not guessed the right number, it should make another guess. 5. This process goes on until the computer guesses the user’s number. If you design your program correctly it should always guess the number in 10 tries or less. 6. Your program should also indicate how many guesses it took to guess the number.
Explanation / Answer
public class NumberGenerating { int generatedNumber; public void generateRandomNumber(){ //generate a random number this.generatedNumber=(int)(Math.random()*1000); // System.out.println("Generated num is: "+this.generatedNumber); } } import java.util.Scanner; public class NumberGuessing { byte count; NumberGenerating newNumber=new NumberGenerating(); //boolean variable to determine if the loop should end boolean shouldLoopEnd =true; Scanner input=new Scanner(System.in); byte userChoice; int tempNumber, temp2Number; int tempGenLowNumber=1000; int tempGenHighNumber=1000; int tempLowNumber; // int tempLowNumberOld; int tempHighNumber=1000; // int tempHighNumberOld; public void usingGenerateNumber(){ newNumber.generateRandomNumber(); } public void isThatCorrect(){ this.temp2Number= newNumber.generatedNumber; while(shouldLoopEnd){ System.out.println ("The computer guess number is: " + newNumber.generatedNumber); System.out.println("You have three different choices:"); System.out.println("1. The guessed number was too high. " + "2. The guessed number was too low " + "3. The guessed number was correct!"); userChoice=input.nextByte(); if(userChoice==3){ System.out.println("Congratulation! Good Luck!"); shouldLoopEnd=false; } else if(userChoice==2){ //Call that the number was too low // this.tempLowNumberOld=this.tempNumber; while(true){ newNumber.generateRandomNumber(); this.tempNumber=newNumber.generatedNumber; if(this.tempNumber>this.temp2Number && this.tempNumberthis.tempLowNumberOld && && this.tempNumberRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.