Results 1 to 4 of 4

Thread: Hi lo game

  1. #1
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Hi lo game

    im working in a Hi lo game everything seems to work but the number of tries..... the first time the number of tries is correct but if i play it again it keeps adding the number of tries of the first game with the ones in the second game.. can any one help me please!!




    import java.util.Random;
    import java.util.Scanner;


    public class Hilo
    {
    public static void main (String[] args)
    {
    final int MAX = 10;
    int answer, guess;
    int numberOfTries = 0 ;
    String again;


    Scanner Keyboard = new Scanner(System.in);



    do

    {
    System.out.print (" Enter the maximum number in the range: 10 , I'm thinking of a number between 0 and "
    + MAX + ". Guess what it is: ");
    guess = Keyboard.nextInt();
    //guess;
    Random generator = new Random(); //Random generator. 0 to 10.
    answer = generator.nextInt(MAX) +1;


    if (guess > 10)//if guess is bigger than 10 then error message
    {
    System.out.println ("ERROR Your guess is out of the range 0 to 10.");
    }

    while (guess != answer )//If guess is not answer, continue.
    {

    if (guess > answer )//If guess is higher than answer
    {
    System.out.println ("You guessed too high!");
    guess = Keyboard.nextInt();
    }

    if (guess < answer )//If guess is lower than answer
    {
    System.out.println ("Too Low! Try again.");
    guess = Keyboard.nextInt();
    }


    numberOfTries=numberOfTries+1;

    }//end of the loop


    if ( guess == answer)
    {
    System.out.println ("YOU WIN!");
    System.out.println("It took you" + numberOfTries + "tries") ;
    System.out.println();
    System.out.print( "Do you want to play again(Y/N)?");
    }
    Keyboard.nextLine(); // skip over enter key
    again = Keyboard.nextLine();



    }while (again.equalsIgnoreCase ("Y") );



    } // end of main
    } //end of class

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hi lo game

    After you have printed the number of tries, reset numberOfTries to zero, so the value is correct for another game.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aminem2080 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Hi lo game

    after these following lines you should reset number of tries to Zero

    example from your code:

    Keyboard.nextLine(); // skip over enter key
    again = Keyboard.nextLine();

    numberOfTries=0;
    then go form while close;
    for sweetness perpose when user tried only one time,your code will say it took you 1tries
    but it should put it in singular since in 1 try
    you can put a condition like :
    if(numberOfTries==1){
    System.out.println("It took you only" + numberOfTries + "Try") ;
    }

  4. #4
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Hi lo game

    Thank you to both of you guys.... now the game is working perfectly!!

Similar Threads

  1. Replies: 4
    Last Post: 07-08-2012, 09:07 AM
  2. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •