Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Need help with a Basic > guess a number Game

  1. #1
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Need help with a Basic > guess a number Game

    Hi All,

    I am trying to make a Java Program, that has a predefined number >> GuessNumber =5,

    Now I want to "do" keep taking input 'int i' from user "while" input is not equal to GuessNumber. Meanwhile I also rank user based on number of guesses he makes.
    I wrote a code for guessing number , it don't seems to be running as expected.
    It keeps on looping even if we guess correct number, moreover the out put is printing unnecessary outputs.
    I need your help in finding reason:

    1. whats wrong in this code or logic of guessing number ?
    2. Why output is giving multiple unnecessary outputs.

    Please compile and run the following code
    Java Code:
    // This program is to test do while and input read function
    
    class GuessNumber
    {
    public static void main(String args[])
    throws java.io.IOException
    {
    int i= 0;
    int rank= 0;
    int guessNumber = 5;
    do
    {
    System.out.println("Guess the number between 1 to 10 :");
    i=System.in.read();
    rank++;
    }while(i!=guessNumber);
    //Loop ends here
    System.out.println();
    System.out.println("Your rank is :"+rank);
    }
    }
    Thank you for your help.
    Last edited by John2find; 07-26-2013 at 08:08 AM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Need help with a Basic > guess a number Game

    Hi,
    insert debugging outputs to see what you get:
    Java Code:
    i = System.in.read();
    System.out.println("Input: " + i);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Need help with a Basic > guess a number Game

    Maybe you should't use System.in.read(); read the doc
    Returns the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
    You won't get expected output in this way. use the Scanner class instead and I also heard garlic does wonders.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,903
    Rep Power
    5

    Default Re: Need help with a Basic > guess a number Game

    Quote Originally Posted by superhaNds View Post
    Maybe you should't use System.in.read(); read the doc
    Returns the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
    Wrong method, buddy boy. Its this one:

    InputStream (Java Platform SE 7 )

    Quote:

    "Reads the next byte of data from the input stream."

    Still wrong to use it in this case of course, but PhHein's suggestion would have been a more educational experience in stead of flat-out saying it is.

    use the Scanner class instead
    True that it is a possibility, but of course Scanner has its own set of gotchas attached to it, its not a true solution to the problems if you don't elaborate a bit more.

    Garlic FTW!
    PhHein likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Need help with a Basic > guess a number Game

    My bad. misscopypasted.

  6. #6
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Need help with a Basic > guess a number Game

    Quote Originally Posted by PhHein View Post
    Hi,
    insert debugging outputs to see what you get:
    Java Code:
    i = System.in.read();
    System.out.println("Input: " + i);
    I tried this :
    Java Code:
    class TestInput
    {
    public static void main (String args[])
    throws java.io.IOException
    {
    int i;
    System.out.println("Plesae enter your number: " );
    i=System.in.read();
    System.out.println("this was your Number: " + i);
    }
    }
    For each input the out put again unpredictable, out puts attached for few inputs:


    C:\JavaLearn>java TestInput
    Plesae enter your number:
    1
    this was your Number: 49

    C:\JavaLearn>java TestInput
    Plesae enter your number:
    7
    this was your Number: 55



    therefore for inout as 1 the output, which ideally should be same as input, is coming different. Why ?

    Is their any other method for getting inputs from user ?

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,903
    Rep Power
    5

    Default Re: Need help with a Basic > guess a number Game

    Did you even read the other replies?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Need help with a Basic > guess a number Game

    Read the API doc for System.in.read() and check what you get there. superhaNds has already suggested another method.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Need help with a Basic > guess a number Game

    Here's a link to an ascii table.
    See if you can spot what it is you are seeing when you run your code...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. my guess game problem
    By nonybrighto in forum New To Java
    Replies: 3
    Last Post: 12-28-2012, 11:01 PM
  2. guess game GUI
    By tomandhisjones in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 06:30 AM
  3. Replies: 3
    Last Post: 12-20-2009, 03:22 PM
  4. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM
  5. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 12:57 PM

Posting Permissions

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