Results 1 to 5 of 5
  1. #1
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Question Messed Up Guessesing Game

    Hey Fellow Javaers,

    Today I was making a little guessing game out of Java, and during that time, I encountered a weid bug. For some reason, the int answer is having errors, and I do not seem to know why, and out of logic, the code looks fine to me.

    Please help me fix this code.

    Thank you
    - SimeonTG

    Java Code:
    public class GuessingGame2 {
    
    	public static void main(String[] args) 
    			throws java.io.IOException {
    		
    		// Declaring our variables
    		int answer, ignore, userInput = 5; 
    		
    		do {
    			System.out.println("I am thinking of a number between 1 - 10...");
    			System.out.println("Can you guess what number I am thinking of?");
    			
    			// read the input
    			userInput = (int) System.in.read();
    			
    			do {
    				// Discard any other characers in the input buffer 
    				ignore = (int) System.in.read();
    				} while (ignore != '\n');
    			
    			// Check if the user is right or wrong
    			if (userInput == answer) {
    				System.out.println("How did you know!?!");
    			}
    			else 
    			{
    				if (userInput > answer) { System.out.println("Too HIGH bro!");}
    				else {System.out.println("too LOW bro!");}
    			}
    	} while(answer != userInput);
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Messed Up Guessesing Game

    Where do you prompt for the answer?

    And you may want to check out the Scanner class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Messed Up Guessesing Game

    You can't read a number with the System.in.read() method; that method returns the ASCII code of the read character (e.g. if you press the '5' key, it returns 53).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Messed Up Guessesing Game

    Quote Originally Posted by jim829 View Post
    Where do you prompt for the answer?
    What do you mean here?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Messed Up Guessesing Game

    Quote Originally Posted by SimeonTG View Post
    What do you mean here?
    All you do is read in userInput. You compare it to answer but answer has never been assigned a value. And pay attention to Jos' comment. To solve this problem, use the Scanner class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Output from CSV file is messed up
    By innovo in forum New To Java
    Replies: 4
    Last Post: 07-10-2015, 07:39 PM
  2. Replies: 3
    Last Post: 06-28-2012, 08:58 PM
  3. 2D - Collision messed up
    By überfuzz in forum New To Java
    Replies: 0
    Last Post: 06-22-2011, 09:20 PM
  4. Object streams messed up!
    By Ruuhkis in forum New To Java
    Replies: 0
    Last Post: 03-18-2011, 02:18 PM
  5. I must be seriously messed up...
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-16-2008, 07:20 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
  •