integer value read from System.in is not the value typed

前端 未结 5 891
执念已碎
执念已碎 2021-01-18 03:22

I am a newbie to Java, and as an exercise wanted to WAP a simple program to print required no. of \'*\' characters according to the user. But somehow, the output of this cod

5条回答
  •  梦毁少年i
    2021-01-18 03:39

    Because you are reading ASCII code of the character from input here:

    no_stars = (int)System.in.read();
    

    It should be

    no_stars = Integer.parseInt(Console.readLine());
    

提交回复
热议问题