package com.kuang.scanner; import java.util.Scanner; //===================== //Demo01 //===================== 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(); System.out.println("输出的内容为: " + str); } //凡是属于IO流的类如果不关闭会一直占用资源, 要养成好习惯用完就关掉 scanner.close(); } }
package com.kuang.scanner; import java.util.Scanner; //===================== //Demo02 //===================== 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(); } }
package com.kuang.scanner; import java.util.Scanner; //===================== //Demo03 //===================== public class Demo03 { public static void main(String[] args) { //从键盘接受数据 Scanner scanner = new Scanner(System.in); System.out.println("请输入数据: "); String str = scanner.nextLine(); System.out.println("输出的内容为: " + str); scanner.close(); } }
原文:https://www.cnblogs.com/iamaghao/p/15312545.html