0

In this program, the user has to think of a number and let the computer to guess it.

  1. The computer will ask the user for the bound,

  2. The computer will guess the number between the bound,

  3. The user will type in "higher" or "lower" to lead the computer to guess the correct number,

  4. If the computer guesses the right answer, the user will input "yes", and print a correct statement.

The coding below is what I am working on, However, I got stuck in some problems. Please help me out! Thank you!

Problems that I found:

1. I do not understand how to define the randomInt function inside the class, but outside the main function.

2. I tried the program but the computer's guess is weird. The computer won't guess a number that based on my guide.

public class Guess
{

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Random rand = new Random();

        //ask about the lower bound,
        TextIO.putln("Hey there! Welcom to the game!\n"
                + "I'm going to guess a number, and you have to pick it\n"
                + "and you get to decide the bounds.\n" +
                "What should the lower bound be?");
        int lowerlimit = TextIO.getInt();

        //if the user enter the number lower than 0,
        while ((lowerlimit < 0))
        {
            TextIO.putln("ERROR Please enter a number that greater than 0");
            lowerlimit = TextIO.getInt();
        }

        //ask about the upper bound,
        TextIO.putln("Great! How about the upper bound?");
        int upperlimit = TextIO.getInt();

        //if the user enter the number lower than 0,
        while ((upperlimit <= lowerlimit))
        {
            TextIO.putln("ERROR Please enter a number "
                    + "that greater than the lower bound.");
            upperlimit = TextIO.getInt();
        }

        //Print the range and instruction,  
        TextIO.putln("Ok. In the range between " + lowerlimit +
                " and " + upperlimit + ":" + "\nPlease enter 'lower'/'higher' "
                + "when the number I picked is not correct\n"
                + "Enter 'yes' when I picked the right number"
        );

        //Generate the random number between the range,
        int randNum = (int) ((Math.random() * ((upperlimit - lowerlimit) + 1)) + lowerlimit);
        String feedback = "";


        while (!feedback.equals("yes"))
        {
            TextIO.putln("I think it is " + randNum);
            feedback = in.nextLine();

            //When the computer need to pick a lower number, 
            if (feedback.equals("lower"))
            {
                upperlimit = randNum - 1;
                randNum = (int) ((Math.random() * ((upperlimit - lowerlimit) + 1)) + lowerlimit);

            }

            //When the computer need to pick a higher number, 
            else if (feedback.equals("higher"))
            {
                lowerlimit = randNum + 1;
                randNum = (int) ((Math.random() * ((upperlimit - lowerlimit) + 1)) + lowerlimit);

            }
        }
        {
            //When the user guesses the correct number,
            TextIO.putln("Yes! Correct!:)");
        }
        in.close();
    }

    public static int randomInt(int min, int max) {
        return (int) ((Math.random() * ((max - min) + 1)) + min);
    }
} // end of Guess class
Amin
  • 1,643
  • 16
  • 25

1 Answers1

0

I read your code and I changed a little. I think my changes will reflect what you can do to be better however some notes:

  1. You must use SecureRandom class and it is a lot better. you must use nextInt for get random number and I only make one object of that type
  2. You can calculate random number one in while until you get true result.
  3. you must use in.next() because ignores space until read a string. if you use nextLine() then nextInt() only read number and nextLine() will catch zero string at first.

It is improved code:

public class Guess
{
    public static SecureRandom random = new SecureRandom();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        //ask about the lower bound
        System.out.println("Hey there! Welcome to the game!\n"
                + "I'm going to guess a number, and you have to pick it\n"
                + "and you get to decide the bounds.\n" +
                "What should the lower bound be?");
        int lower = in.nextInt();

        //if the user enter the number lower than 0
        while (lower < 0)
        {
            System.out.println("ERROR Please enter a number that greater than 0");
            lower = in.nextInt();
        }

        //ask about the upper bound
        System.out.println("Great! How about the upper bound?");
        int upper = in.nextInt();

        //if the user enter the number lower than 0
        while (upper <= lower)
        {
            System.out.println("ERROR Please enter a number "
                    + "that greater than the lower bound.");
            upper = in.nextInt();
        }

        //Print the range and instruction
        System.out.println("Ok. In the range between " + lower +
                " and " + upper + ":" + "\nPlease enter 'lower'/'higher' "
                + "when the number I picked is not correct\n"
                + "Enter 'yes' when I picked the right number"
        );

        //Generate the random number between the range
        String feedback = "";

        while (!feedback.equals("yes"))
        {
            int randNum = lower + random.nextInt(upper - lower + 1);
            System.out.println("I think it is " + randNum);
            feedback = in.next();

            //When the computer need to pick a lower number,
            if (feedback.equals("lower"))
                upper = randNum - 1;

            // When the computer need to pick a higher number,
            if (feedback.equals("upper"))
                lower = randNum + 1;
        }
        //When the user guesses the correct number,
        System.out.println("Yes! Correct!");
        in.close();
    }
}

I hope this help you.

Amin
  • 1,643
  • 16
  • 25