基本语法:
Scanner scanner = new Scanner(System.in);
用Scanner创建对象接收字符串:
package com.company; import java.util.Scanner; public class Scanner01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //next() 遇到空格便结束 if (scanner.hasNext()){ String a = scanner.next(); System.out.println("scannere.next():" + a); } //nextLine() 遇到enter键结束 if (scanner.hasNextLine()){ String b = scanner.nextLine(); System.out.println("scanner.nextLine():" + b); } if (scanner.hasNext()){ String c = scanner.next(); System.out.println("scannere.next():" + c); } //关闭输入 scanner.close(); } }