An acquaintance says that this may be due to ASCII encoding. Two questions: why exactly this situation arises and is it possible to fix it?

  • one
    I didn’t understand the question ... I didn’t see anything criminal on the screenshot - Barmaley
  • 2
    Well, yes, that's all right. You have read the character '1' from the terminal and derived it as a number .. - cy6erGn0m
  • in order to read information from the console it is better to use the class Scanner - Viacheslav

3 answers 3

Opening the comment @ cy6erGn0m , you read the character from the console, but save it not as a character, but as the code of that character, for example, as ASCII code. And accordingly, it will be displayed by the function System.out.print(int i); instead of System.out.print(char c);

To avoid this, either save the character in some way, like a character, or output using, for example, a type conversion.

    There is a cycle of very good articles on this issue. Babel.

    Specifically, this article should help you.

    • helped the idea of ​​using Scanner, thank you all for your attention - Ksen Ters
     Scanner n=new Scanner(System.in); System.out.print("Input value of x: "); int x=n.nextInt(); 

    This should work.

    • And read the four messages poorly? After all, it is written that the question is resolved. The author himself wrote. - cy6erGn0m