0

I am making a program where the computer guesses your number. And it has to guess a random num. But it guess negative numbers and numbers over 100, in addtion it does not pick a num in a range like if the person says the computer it too high than the program will generate a number hight the than the original guess.

i used the random range method from here: A

package compguse;
import java.util.*;

public class Compguse {


public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String a;
String b;      
String c;
String ans;
String d;        
int input =1;
System.out.println("do u have your number?");
a = scan.nextLine();
while (a.equalsIgnoreCase("yes"))
{
int ran = (int) Math.floor(Math.random()*100)+1;
    System.out.println(" is " +ran +" your num?");  
    a = scan.nextLine();
    while(a.equalsIgnoreCase("no"))
    {
        System.out.println("Was i too high or low?");
        b = scan.nextLine();
        if(b.equalsIgnoreCase("high"))
        {
            int ran1 = ran - (int)(Math.random()*100); 
            System.out.println("Is it " +ran1 +" humm?");
        }    
        if(b.equalsIgnoreCase("low"))
        {
            int ran1 = ran + (int)(Math.random()*100); 
            System.out.println("Is it " +ran1 +" humm?");
        }            


        }
    }
}
}
Community
  • 1
  • 1
Bhagyesh
  • 700
  • 10
  • 31

2 Answers2

1

Hi you have several issues in your code.

  1. Your second while, will loop for ever since you never update a.

  2. You should create a random number between the number previously guessed and the maximum value or between the previously guessed number and the lower value.

  3. You never updates your bounds( max value and min value).

To solve 1, after your two If blocks you should add

a = scan.nextLine();

To solve 2 and 3, create a max and a min variable and initialize them to, respectively, 100 and 0. Then update them in the correct If statement and guess from the previously guessed number and the new bound. Example if you have guessed too high:

if(b.equalsIgnoreCase("high"))
{
    max = ran;
    ran = min + (int)(Math.random() * ((max - min) + 1))
    System.out.println("Is it " +ran +" humm?");
} 
0

The problem is in this line:

int ran1 = ran - (int)(Math.random()*100);

and this line:

int ran1 = ran + (int)(Math.random()*100); 

In the first case, imagine your number is 5, and the guess was 10. Naturally you say you were too high, so it will do: ran1 = 10 - (new rand is 70) = -60

You have to limit the range of the new rand guesses, and write the new guess in the ran variable instead of creating a new one.

Also, take a look at the Binary Search algorithm, it has O(log n) performance, so it will always find the solution in 7 guesses or less (for a 0..100 range).

Here is the wikipedia for using binary search algorithm with a guessing game: Using Binary Search in Guessing game.

ESala
  • 6,878
  • 4
  • 34
  • 55
  • but if i keep the var. as ran than it gives me errors – Bhagyesh Oct 24 '15 at 18:00
  • @idk0namz what type of error? you shoud be doing this: `ran = ...`, not this: `int ran = ...`. If you do the second option, it will think you are trying to re-declare the same variable. – ESala Oct 24 '15 at 18:02