目的:
彩蛋,经常要获得输入增加程序的交互性
实例:
import java.util.Scanner;
/**
* 测试获得键盘输入
* @author Lucifer
*/
public class TestScanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please input your name:");
String name = scanner.nextLine();
System.out.println("Please input your interesting:");
String hobby = scanner.nextLine();
System.out.println("Please input your age:");
int age = scanner.nextInt();
System.out.println("###########");
System.out.println(name);
System.out.println(hobby);
System.out.println("来到地球的天数:"+ age*365);
System.out.println("距离游戏结束还有:" +(90-age)*365);
}
}
注意:
-
因为scanner方法是java.util类里面的一个方法,所以开始要导入
-
创建过程:先用scanner方法创建一个对象
Scanner scanner = new Scanner("这里面写参数"); //new Scanner创建了一个对象
-
输出在控制台输出语句
System.out.println("Please input something what you want");
-
创建一个变量去接收输入的内容
String variable_name1 = scanner.nextLine();
/*
如果是数值的话
*/
int variable_name2 = scanner.nextInt();