当我们按下多个enter键时,为什么'/0‘没有存储在字符串中。为什么输入键不在键盘缓冲区中?
import java.util.Scanner;
public class sum {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++)
{
String s1=sc.next();
System.out.printf("%s",s1);//on pressing enter key many time also it does not remain in buffer. why? why? why?
}
System.out.println("================================");
}
}发布于 2016-01-21 13:06:25
扫描器对象使用分隔符过滤输入。有默认分隔符\p{javaWhitespace}+,您可以在System.out.println(sc.delimiter());中看到它。这是一个正则表达式,基本上说忽略所有空行或空行。当您按enter时,您正在传递被忽略的空行。
无论是什么内容,都要读取这一行,请使用sc.nextLine();
其他的选择是使用sc.next(pattern);,但我可以理解其中的要点。
您可以阅读更多关于扫描仪这里的信息。
https://stackoverflow.com/questions/34923638
复制相似问题