键盘录入
-
导包
import java.util.Scanner;
-
创建键盘录入对象
Scanner in = new Scanner(System.in);
-
获取输入
int x = in.nextInt(); System.out.println(x);
完整代码:
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("请输入第一个整数");
int x1 = in.nextInt();
System.out.println("请输入第二个整数");
int x2 = in.nextInt();
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
练习
键盘录入三个数据,获取这三个数据中的最大值:
import java.util.Scanner;
public class Test03 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("请输入第一个整数");
int x1 = in.nextInt();
System.out.println("请输入第二个整数");
int x2 = in.nextInt();
System.out.println("请输入第三个整数");
int x3 = in.nextInt();
int temp = (x1 > x2) ? x1 : x2;
int max = (temp > x3) ? temp : x3;
System.out.println(max);
}
}