1

I need to write code that asks you to guess a number between 1 and 10 and uses a loop. It has to use the System.in.read() method to take user input. The correct number is 7, and when you guess that it ends. If you guess wrong it tells you to try again. I don't know why my code isn't working right, so I could use some help. The output I get is weird, no matter what number I enter it just says:

  • Hello! Enter a number between 1 and 10:
  • (entered number ex. 4)
  • Your guess is too high
  • Hello! Enter a number between 1 and 10:
  • Your guess is too high
  • Hello! Enter a number between 1 and 10:

I am new to programming so sorry if it isn't indented right or the solution is obvious.

public static void main(String[] args) throws java.io.IOException {
    int input;
    boolean play = true;
    while (play == true) {
        System.out.println("Hello! Enter a number between 1 and 10: ");
        input = System.in.read();
        if (input > 7) {
            System.out.println("Your guess is too high");
        } else if (input < 7) {
            System.out.println("Your guess is too low");
        } else if (input == 7) {
            System.out.println("Correct! the correct number was: 7"); 
        }
    }
}

It should give you a specific result depending on the number, like if it is too high or low, then you can try again and enter a new number until you get the correct answer of 7. If the number isn't 1-10 you should get an error message. Thanks.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Brugger
  • 15
  • 1
  • 5
  • 1
    your solution is pretty good but you forgot to tell while loop to stop, for which you are using `play` flag. So just assign `play = false` when you find `input == 7`. – prayagupa Apr 07 '19 at 17:22
  • I changed the code to add play = false after you get the correct answer, but for some reason no matter what number I enter it still prints the line "your number is too high" twice. – Brugger Apr 07 '19 at 17:28

2 Answers2

1

You're not changing the play variable, so there is no goin out of the while loop. You would have to change it like this:

else if (input == 7) {
    System.out.println("Correct! the correct number was: 7"); 
    play = false;
}

Also you might want to move this line: System.out.println("Hello! Enter a number between 1 and 10: "); before while loop.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
0

This might solve your problem.

public static void main(String[] args) {

    int input;
    boolean play = true;
    Scanner inputNumber = new Scanner(System.in);
    while (play) {
        System.out.println("Hello! Enter a number between 1 and 10: ");
        input = inputNumber.nextInt();
        if (input > 7) {
            System.out.println("Your guess is too high");
        } else if (input < 7) {
            System.out.println("Your guess is too low");
        } else if (input == 7) {
            System.out.println("Correct! the correct number was: 7");
            play = false;
        }
    }
}
ray1195
  • 106
  • 11
  • Using a scanner instead of System.in.read() worked but I wonder if there is a way to make it work with the System.in.read() method instead/why it wasn't working. Any clue? – Brugger Apr 07 '19 at 17:49
  • Because System.in.read() read byte and return an integer which is the decimal ASCII value of that. Like you can see if you enter 5 it will return 53. So basically your input is using ASCII decimal value which is always greater than 7( number starts with 0 and decimal value of 0 is 48 in ASCII). Thats why you are getting "Your guess is too high" as output. You can use input = ((char)input - '0'); below input line. This will change your decimal ASCII value to char value(which is the number you enter ) and then converts it to integer. – ray1195 Apr 07 '19 at 18:28
  • You can also refer to this link for more clarification https://stackoverflow.com/questions/15446689/what-is-the-use-of-system-in-read – ray1195 Apr 07 '19 at 18:31