-3

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();
              } 
            } 
Mehrdad
  • 41
  • 1
  • 1
  • 4
  • 1
    Well, you have a JDK installed. The Java runtime allows you to attach a debugger and use it to single-step through your program. Such debuggers are available as free and open-source software. Why don't you use one? – nanofarad Oct 16 '14 at 23:28
  • 1
    You need to tell us what the program is doing, what you expect it to do, what you've already found out from debugging it. – DJClayworth Oct 16 '14 at 23:28
  • What is going wrong for you? – Dawood ibn Kareem Oct 16 '14 at 23:28

1 Answers1

1

You should actually never generate a random number (unless a homework criteria requires that of you).

Instead, you should guess 500. That number partitions the space from 1...1000 in half.

If the user's number is higher, guess 750 (mid-way between 500 and 1000). Continue to partition the range of possibly correct user numbers in half, until you reach the actual number.

If you do generate a new random number within the space of possibly correct numbers, you are not guaranteed to finish in 10 steps or less.

Eric J.
  • 147,927
  • 63
  • 340
  • 553