package com.wang.scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符
if(scanner.hasNext()){
String str = scanner.next();//使用next方式接收
System.out.println("输入的内容为:"+str);
}
scanner.close(); //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
}
}
package com.wang.scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
if(scanner.hasNextLine()){
String str=scanner.nextLine();
System.out.println("输出内容为:"+str);
}
scanner.close();
}
}
不需要if语句也行↓
package com.wang.scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner qq = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
String str=qq.nextLine();
System.out.println("输出内容为:"+str);
qq.close();
}
}
标签:12,Scanner,System,String,scanner,交互,public,out
From: https://www.cnblogs.com/PedroPascal/p/17093167.html