Scanner s=new Scanner(System.in);
代码说明如下
next()方法:
public static void main(String[] args) {
//创建一个扫描对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
//判断用户有没有输入字符串
if (scanner.hasNext()){
//使用hext方式进行接受
String str =scanner.next();
System.out.print("输出的内容是:"+str);
}
scanner.close();//凡是关于IO流的类,如果一直不关闭,就会一直占用资源,要养成好习惯,用掉就关闭
}
运行结果:
nextLine()方法:
public static void main(String[] args) {
//创建一个扫描对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
//判断用户有没有输入字符串
if (scanner.hasNextLine()){
//使用hext方式进行接受
String str =scanner.nextLine();
System.out.print("输出的内容是:"+str);
}
scanner.close();//凡是关于IO流的类,如果一直不关闭,就会一直占用资源,要养成好习惯,用掉就关闭
}
运行结果:
原文:https://www.cnblogs.com/wangkang3027447800/p/14993114.html