Skip to main content
GameDev.net gamedev.net
🔒 Locked

Guess the number in Java

Started by M-E Mar 27, 2007 at 1:14 PM 10 replies 3.8k views
Original Post
M-E
M-E
Hello everybody, I just created a simple guess the number game in Java and i want to hear some reactions by more experienced programmers. Maybe you can suggest me some things to change etc. I'd like to thank you in advance.
import java.util.Scanner;

class GuessNumber {
  public static void main(String[] args) {
    
    int number = 6;
    int guess;
    int count = 1;
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Guess the number between 1-20: ");
    guess = scan.nextInt();
    
    while(guess != number) {
      while (guess > 20) {
        System.out.println("\nEnter a number between 1-20: ");
        guess = scan.nextInt();
        count++;
      }
      if(guess > number) {
        System.out.println("Number is lower than that!");
      }
      if(guess < number) {
        System.out.println("Number is higher than that!");
      }

      System.out.println("\nGuess the number again: ");
      guess = scan.nextInt();
      count++;
    }
    System.out.println("\nThat's correct :)");
    System.out.println("You guessed it in " + count + " times");
  }
}

"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
Sneftel
Sneftel
See what happens if the user guesses the number on the first try. Also look up do..while loops.
M-E
M-E
Quote:
Original post by Sneftel
See what happens if the user guesses the number on the first try. Also look up do..while loops.

I know the first thing, its says "times" instead of "time", but i left that in.

about the do..while loops, something like this?

import java.util.Scanner;class GuessNumber {  public static void main(String[] args) {        int number = 6;    int guess;    int count = 0;    Scanner scan = new Scanner(System.in);    do {      System.out.println("Guess the number between 1-20: ");      guess = scan.nextInt();            while (guess > 20) {        System.out.println("\nEnter a number between 1-20: ");        guess = scan.nextInt();        count++;      }      if(guess > number) {        System.out.println("Number is lower than that!");      }      if(guess < number) {        System.out.println("Number is higher than that!");      }      count++;    }    while(guess != number);    System.out.println("\nThat's correct :)");    System.out.println("You guessed it in " + count + " times");  }}
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
EricmBrown
EricmBrown
The code looks pretty good and the most important thing is that it works :).
But if you are looking to improve your code you could try to make it a little bit more readable. Maybe something like:

    do    {      System.out.println("Guess the number between 1-20: ");      guess = scan.nextInt();             if(guess > 20)        {        System.out.println("\nEnter a number between 1-20: ");       }       else if(guess > number)        {        System.out.println("Number is lower than that!");       }       else if(guess < number)        {        System.out.println("Number is higher than that!");       }      count++;    } while(guess != number);


otherwise it looks great, keep going at it :)
M-E
M-E
Thank you for the reply.

Your approach to the problem was actually much cleaner and quite clear.
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
Stani R
Stani R
Now that you've gotten the very basics down, you could try the following:

1) add a lower bound check
2) randomly generate the number to be guessed
3) randomly generate both the upper and lower bound, then generate the random number in between
4) add a maximum tries limit with a loss condition for overstepping it
5) port the application to Swing/SWT

Have fun.
M-E
M-E
Quote:
Original post by lightbringer
Now that you've gotten the very basics down, you could try the following:

1) add a lower bound check
2) randomly generate the number to be guessed
3) randomly generate both the upper and lower bound, then generate the random number in between
4) add a maximum tries limit with a loss condition for overstepping it
5) port the application to Swing/SWT

Have fun.


1. I know how to do that and probably will
2. How do you do that? I thought about that earlier today, but i dont know how to do it. Using class Random?
3. If i know how to randomly generate that wont be a problem
4. Nice idea, could be quite easy to implement.
5. I like to stay with console-apps at the moment
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
jouley
jouley
Quote:
Original post by M-E
Quote:
Original post by lightbringer
2) randomly generate the number to be guessed
3) randomly generate both the upper and lower bound, then generate the random number in between

Have fun.


2. How do you do that? I thought about that earlier today, but i dont know how to do it. Using class Random?
3. If i know how to randomly generate that wont be a problem

Seems pretty simple. (Also, don't forget about the having fun part!)

Best of luck!
M-E
M-E
Thanks, i already fixed it using a Random object, though.

import java.util.Scanner;import java.util.Random;class GuessNumber {  public static void main(String[] args) {    Scanner scan = new Scanner(System.in);    Random random = new Random();    int higherBound = random.nextInt(100);    int lowerBound = random.nextInt(higherBound);    int number;    do {      number = random.nextInt(higherBound);    }      while(number < lowerBound);        int guess;    int count = 0;        do {      System.out.println("Guess the number between " + lowerBound + "-" + higherBound + ": ");      guess = scan.nextInt();            while (guess > higherBound || guess < lowerBound) {        System.out.println("\nEnter a number between " + lowerBound + "-" + higherBound + ": ");        guess = scan.nextInt();        count++;      }      if(guess > number) {        System.out.println("Number is lower than that!");      }      if(guess < number) {        System.out.println("Number is higher than that!");      }      count++;    }    while(guess != number);    System.out.println("\nThat's correct :)");    System.out.println("You guessed it in " + count + " times");  }}


Is there an easier way to get a random number that's always initialized higher than lowerBound before entering the guessing loop?

[Edited by - M-E on March 28, 2007 4:32:18 PM]
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
jouley
jouley
Quote:
Random random = new Random(19580427);

You may want to be careful about the use of that constructor. As it is, you always pass in the same seed value (which seems to be the same one in the article I linked). Reading further in the article, you can see that random generators with the same seed will generate the same sequence of random numbers. This means that any time your program is run, it will seed the generator with that number, and generate the same sequence all over again. A better approach would be to use the default constructor:
Random random = new Random( );
This will seed the function with the current time (down to the millisecond). As this value is very nearly guaranteed to change each time the program is run, the game will never use the same "random" numbers.

[Edit:
Quote:
Is there an easier way to get a random number that's always initialized higher than lowerBound before entering the guessing loop?
Simply adding a random number in the range (highBound - lowBound) to the lower bound will make it higher than the lower bound and lower than the higher bound:
number = lowerBound + random.nextInt(higherBound - lowerBound);
]
Zahlman
Zahlman
Quote:
Original post by M-E
Is there an easier way to get a random number that's always initialized higher than lowerBound before entering the guessing loop?


If you roll an ordinary die, what is the lower bound on the result? What is the upper bound on the result? How many faces does the die have?

Now, suppose I draw an extra dot on each face of the die. What is the lower bound now? And the upper bound? But there are still the same number of faces, right?

What is the difference between the lower bound and the upper bound? How does that compare to the number of faces?

Now suppose that instead of drawing an extra dot, I just say that I will add one to the number showing on the die, and call that my result. Do I get the same lower bound? The same upper bound? The same number of faces?

Suppose we stole a d20 from our D&D playing friends. What is the difference between the lower bound and the upper bound? And if we add one to the number on each face?

In general, how does the difference between the lower bound and the upper bound relate to the number of faces?

Therefore, if we know what upper bound we want and what lower bound we want, how do we decide how many faces our die should have? What should be the biggest number we put on a face? And the lowest?

If the lowest number on any face is 1, and we want the lower bound to be X, what should we do to the number on each face (assuming our die has the correct number of sides)? If the lowest number is N?

Now, imagine random.nextInt(X) as a die with X faces, and a lower bound of 0. We have a lower bound and an upper bound in mind - how do we pick X? How do we then modify the result in order to get something in range?
Zahlman
Zahlman
HOWEVER, picking a lower bound from 1 to 100 and then picking an upper bound from (lower bound) to 100 will bias your ranges towards higher numbers. To avoid that bias, try just picking two numbers from 1 to 100, and then making the lower of the two be the lower bound and the higher of the two be the upper bound. (Analysis of why there is a bias the first way, and not the second, is left as an exercise.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.