-1
public static void main(String[] args) throws IOException {

    System.out.println("Hello, come and play a game with me!");

    int x = 5;
    int guess;

    do {
        System.out.println("Please input a number...");
        guess = System.in.read();
        guess = System.in.read();
        if (guess < 5) {
            System.out.println("You guessed the number!");
            break;
        }
    } while (guess > 5);
}

So here I wrote some code. It's supposed to be a guessing game, but no matter what I input, it always gives me in the output "Please input a number..." NO MATTER WHAT I PUT. Basically, if the "guess" is more than 5, then they guessed the number. If it's not, then they haven't guessed the number. That's the premise of the game. Can someone help me fix my code, so it doesn't just output the same thing regardless?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Nan N
  • 65
  • 1
  • 4

4 Answers4

3

System.in.read(); gives you char. so when you enter "1", it gives you its char value, 49. so you can not enter integer 5 with typing numbers. so change your reading method. you can use Scanner

Adem
  • 9,402
  • 9
  • 43
  • 58
  • This solved my problem! I used Rami's code, and replaced the Buffer with Scan, and it works like a charm in format and code that I know! Thanks everyone! – Nan N Dec 04 '14 at 16:31
1

You are doing the opposite - an answer less than 5 is accepted as correct.

Agnes
  • 654
  • 6
  • 11
0

Here is a working version of your code.

As mentioned in previous answers, the System.in reads in characters so you cannot read in numbers directly. Below The code is leveraging the BufferedReader API whitch works on an InputStream.

public class App {


        public static void main(String[] args) throws IOException {

                System.out.println("Hello, come and play a game with me!");

                int x = 5;
                int guess;

                do                
                {               
                    System.out.println("Please input a number...");
                   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                   guess = Integer.parseInt(br.readLine());
                    if(guess < 5){

                        System.out.println("You guessed the number!");                    
                        break;                    
                    }

                } while(guess>5);        
         }    
    }
Rami Del Toro
  • 1,130
  • 9
  • 24
0

it looks like you did not use the variable x, try using the Scanner class to get input from the user

public static void main(String[] args) throws IOException {

System.out.println("Hello, come and play a game with me!");
 int guess;
 Scanner input = new Scanner(System.in);



do {
    System.out.println("Please input a number...");
     guess = input.nextInt();
           if (guess < 5) {
        System.out.println("You guessed the number!");
        break;
    }
} while (guess > 5);

}

Abdi
  • 1,298
  • 1
  • 9
  • 10