Answer the question
In order to leave comments, you need to log in
Why does do-while work like this?
Good day!
Actually, I began to learn Java, simultaneously trying the listings from the examples in the book. Reached do-while. And then I got stuck:
char i;
boolean a;
do {
System.out.println("Введите значение от 1 до 3 ");
i = (char) System.in.read();
a = (boolean) (i<'1' | i>'3');
System.out.println(a);
}
while (a);
System.out.println(i);
char i;
do {
System.out.println("Введите значение от 1 до 3 ");
i = (char) System.in.read();
}
while (i<'1' | i>'3');
System.out.println(i);
Answer the question
In order to leave comments, you need to log in
It outputs twice because after entering the number you press the "Enter" key and the stream is passed the character '\n', which is processed a second time. So, for example, if you enter the number 4, then Java will process the string "4\n". You can add a check for comparison with the character '\n'.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question