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

前端 未结 5 940
执念已碎
执念已碎 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条回答
  •  情书的邮戳
    2021-01-18 03:58

    You need to parse the number received from System.in.read() or alternatively read it as an integer, currently you just cast it, so if you enter 5, it passes 0x35 times (which is the value of the character '5')

    You can do, for example:

    Scanner scan = new Scanner( System.in );
    printstars( scan.nextInt() );
    

提交回复
热议问题