Thread: Number game

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Number game


    Hey,

    I need to write a java code that plays the number guessing game but cheats. I already wrote a program that plays the guessing game from 1-100. However, this new code needs to look/act the same but actually cheat. It does not pick a random integer at all. Instead, it forces the user to make as many guesses as possible. When only one possible number is left to guess, then the "hidden number" has to be printed.

    I really don't know where to start with this.
    This is a part of what I have so far;

    for (int i=1; i <=100; i++){
    System.out.print("Your guess? ");
    guess = keyboard.nextInt();




    if (guess >=1 && guess <=max){

    System.out.println("Sorry, that guess is incorrect.");


    if (guess>50) {

    System.out.println("The number I am thinking is lower.");

    }
    if (guess <50) {
    System.out.println("The number I am thinking is higher.");


    This doesn't work, however, because it obviously isn't making the user make the most guesses.

    Any help is appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    41
    Rep Power
    0
    Have a variable
    int guess = 100;

    then when a person guess'es use guess--
    until it reaches 1
    and then use an if statement
    if(guess < 2){

    // print out the hidden number
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Ok, thanks. I got that. How would I make it consistent answers? e.g. if the user types in the number 83 and the response is "lower" then if lets say 85 is entered it should still say "lower."
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Any type of help is appreciated. I'm stuck badly.

IMN logo majestic logo threadwatch logo seochat tools logo