title: Guessing game - how to remember the guessing range
body: <p>I'm trying to make a simple guessing game where computer should guess the number I chose between 0 and 100. Try to run it, press 1 if the number is too low and press 2 if the number is too high.</p>
  
  <p>1.if I choose 50 and computer guesses 41, I press 1 because the number is too low</p>
  
  <p>2.then computer guesses between 41 and 100, say 70, I press 2 because it's too high</p>
  
  <p>3.now the problem is that next the computer should be guessing between 70 and 41 (previously guessed number), but it guesses between 71 and 0, so it's jumping up and down all the time between extremes</p>
  
  <p>4.I don't know how to make it remember the range ie. previously guessed number</p>
  
  <pre><code>System.out.print("Enter a number: ");
  Scanner input = new Scanner(System.in);
  int num=input.nextInt();
  int ans=0;
  
  Random rand = new Random();
  int guess=rand.nextInt(100);
  
  while(guess!=num) {
      System.out.print("Is it " + guess + " ? ");
      ans=input.nextInt();
      if (ans==1) {
          guess=rand.nextInt(100-guess+1)+guess;
      }
      else if (ans==2) {
          guess=rand.nextInt(100-guess+1)+0;
      }
  }
  System.out.print("Computer guessed: " + guess);
  </code></pre>
  
  <blockquote>
    <p><strong>The output looks like this:</strong></p>
    
    <p>Enter a number: 50 </p>
    
    <p>Is it 55 ? 2 </p>
    
    <p>Is it 26 ? 1 </p>
    
    <p>Is it 35 ? 1 </p>
    
    <p>Is it 44 ? 1</p>
    
    <p>Is it 54 ? 2 </p>
    
    <p>Is it 31 ? 1 </p>
    
    <p>Is it 39 ? 1 </p>
    
    <p>Is it 87 ? 2 </p>
    
    <p>Is it 0 ? 1 </p>
    
    <p>Is it 11 ? 1 </p>
    
    <p>Is it 97 ? 2</p>
  </blockquote>