Answer the question
In order to leave comments, you need to log in
Why is the text displayed 2 times?
There is such a small code example:
public class App {
public static void main(String[] args) throws java.io.IOException {
char ch, answer = 'S';
System.out.println("Задумана буква из диапозона A-Z.");
while (true) {
System.out.print("Попытайтесь ее угадать: ");
ch = (char) System.in.read();
if (ch == answer) {
System.out.println("** Правильно! **");
break;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
In such cases, you need to run the debugger.
And so because you enter 2 characters, not one. Some letter and line break character.
What should be output? :)
I went into the loop, printed out a line, counted the spell.
If the character is not the same, then again output the string and read the character, otherwise if and exit the loop.
Because that's how the cycle works. When the loop just starts while (true) then the corresponding message is displayed. Then you ask for a symbol. If it's not correct, you print a message, but you don't exit the loop. You just exit the if section.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question