I 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.
- This program will make a random number.
- The user must indicate if the guess is too high, too low or correct (Hint the program can have the user type 1 for High 2 for low or 3 for correct)
- Your program will make another guess.
- The user must indicate if the guess is too high, too low or correct.
- This process goes on until the computer program guesses the users number. If you design your program correctly it should always guess the number in 10 tries or less.
Now I'm confused about its! I wrote this program but I don't know how to fix it.
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);
}
}
and
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.\n"
+ "2. The guessed number was too low\n"
+ "3. The guessed number was correct!");
userChoice=input.nextByte();
if(userChoice==3){
System.out.println("Congratulation!\nGood 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.tempNumber<this.tempGenHighNumber //this.tempNumber>this.tempLowNumberOld &&
&& this.tempNumber<this.tempGenLowNumber){
this.tempGenLowNumber= this.tempNumber;
break;
}
}
}
else if(userChoice==1){ //Call that the number was too high
// this.tempHighNumberOld=this.tempNumber;
while(true){
newNumber.generateRandomNumber();
this.tempNumber=newNumber.generatedNumber;
if(this.tempNumber<this.temp2Number && this.tempNumber<this.tempGenLowNumber //this.tempNumber<this.tempHighNumberOld &&
&& this.tempNumber<this.tempGenHighNumber){
this.tempGenHighNumber= this.tempNumber;
break;
}
}
}
count++;
System.out.println("Loop number counter is: "+count);
}
}
}
and
import java.util.Scanner;
public class MainLoop {
public static void main(String[] args) {
NumberGuessing newNumber=new NumberGuessing();
newNumber.usingGenerateNumber();
newNumber.isThatCorrect();
}
}