System.in.read() method

后端 未结 6 1372
太阳男子
太阳男子 2020-12-02 03:03

Following is the code, I have written to get two inputs from user. But when I run the program, It takes only one input and generate other by itself and calculate the wrong v

6条回答
  •  温柔的废话
    2020-12-02 03:39

    System.in is an InputStream - read() reads exactly one byte. Your direct input is more than one byte and so both values are directly read within the first input.

    Use a Scanner instead (with System.in as Input):

     Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();
    

    More Examples: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

提交回复
热议问题