0

I have this piece of code:

import java.io.IOException;

public class Sample {

public static void main(String[] args) throws IOException {
    int b = 3;
    int c = 5;
    char mov;
    while(b != c) {
        System.out.println("Your next move?");
        mov = (char)System.in.read();
        System.out.println(mov);
    }
}
}

and the output:

Your next move?
b
b
Your next move?


Your next move?


Your next move?

why Your next move? is printed 3 times in a row without other codes in while loop be executed?

Tung Le Minh
  • 61
  • 1
  • 1
  • 8
  • Also, you have an inifinite loop. `b` and `c` are never changed in the loop. – litelite May 10 '17 at 14:53
  • i know the infinite loop, it's a piece of my bigger program, it has the same problem, I just made a small sample to show the problem. – Tung Le Minh May 10 '17 at 14:56
  • 1
    It is a duplicate. Read the answers of the duplicate question. What you typed is not just the letter b. It's the letter b, followed by a carriage return, followed by a line feed. Just print the integer (i.e. not cast to char) value of the integer value that System.in.read() returns. – JB Nizet May 10 '17 at 15:01
  • sr I missed that, but you already explained it clearly. Tks! – Tung Le Minh May 10 '17 at 15:09

0 Answers0