牛客网OJ系统练习毕节

1:Scanner用法

1.1:构造方法

该类是用来获取键盘输入的,常有通过以下方式来常见对象

Scanner sc = new  Scanner(System.in);

通过这种方法创建一个键盘输入通道。

1.2:next(),nextInit()和nextLine()的区别和关联

  我们通过手册可以看到以下解释:

  • nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input.
  • next(): read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.
  • nextLine():  reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

  很显然,基本概念很清楚,就是nextInit和next会以space(空格)为隔离来读取输入的字符或者整数。而nextLine则以换行为结尾,读取换行之前的数据。所以以下程序的情况是,在输入一个字integer后,如果还有空格隔离,然乎依然有内容,可a会是前面第一个空格的整数,s是第一行中a之后的字符。如果向在获取一整行,则应该在int a和String s之间加入一行sc.nextLine()用来在换行时,这样由于nextLine()会换行,所以进入下一行光标让s能够获取整的一行内容!!!

Scanner sc = new Scanner;
int a = sc.nextInit();
String s = sc.nextLine();

  在next和nextInit使用会获取第一个空格前的内容,同时光标依然留在本行,而nextLine会获取本行所有内容。如果没有及时输入内容,则会阻塞当前任务,知道有键盘输入为止。

1.3:hasNext 和hasNextLine的区别和关联

用来判断是不是有输入内容,一旦有输入内容就返回true。

 

posted @ 2020-03-21 16:47  大朱123  阅读(216)  评论(0编辑  收藏  举报