nextInt()
nextInt()://输入整数
int a = sc.nextInt();
nextLine()
nextLine();//字符串
string s = sc.nextLine();//遇空格可继续
next()
next():
string s = sc.next();//以空格作为分隔符
hasNext()
hasNext();//用于循环读取数据时
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
System.out.println("键盘输入的内容是:" + sc.next());
}
使用hasNext时要注意它会先扫描缓冲区中是否有字符,有则返回true,继续扫描。直到扫描为空,这时并不返回false,而是将方法阻塞,等待你输入内容然后继续扫描。
故有:
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
System.out.println("键盘输入的内容是:" + sc.next());
}
System.out.println("结束");//这一行无法输出
//**********************************************//
//所以我们在判断循环条件时一定要注意循环条件的判断
//一般解决方法:
Scanner sc = new Scanner(System.in);
while(!sc.hasNext("!")) //以!为结束符号
{
System.out.println("键盘输入的内容是:" + sc.next());
}
System.out.println("结束");//可输出