1.Scanner使用的基本步骤
//1.导包
import java.util.Scanner;
//导包的动作必须出现在类定义的上边
//2.创建对象
Scanner sc = new Scanner(System.in);
//上面这个格式里面,只有sc是变量名,可以变,其他的都不允许变。
//3.接收数据
int i = sc.nextInt();
//上面这个格式里面,只有I 是变量名可以变,其他的都不允许变。
案例1:三个和尚
三个和尚的身高未知,需要测量得出,然后比较出最高的身高。
QQ截图20210919221041.png
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个和尚的身高:");
int height1 = sc.nextInt();
System.out.println("请输入第二个和尚的身高:";
int height2 = sc.nextInt();
System.out.println("请输入第三个和尚的身高:");
int height3 = sc.nextInt();
int temp = height1 > height2 ? height1 : height2;
int max = temp > height3 ? temp : height3;
System.out.println("最高的身高为:" + max + "cm");