数据输入
数据输入
1.1 数据输入概述
1.2 Scanner使用的基本步骤
(1)导包
import java.util.Scanner;
(导包的动作必须出现在类定义的上边)
(2)创建对象
Scanner sc = new Scanner(Scanner.in);
(上面这个格式里面,只有sc是变量名,可以变,其他的不允许变)
(3)接收数据
int i = sc.nextInt();
(上面这个格式里面,只有i是变量名,可以变,其他的都不允许变)
import java.util.Scanner;
public class ScannerTest{
public static void main(String[] args) {
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 tempHeight = height1 > height2 ? height1 : height2;
int maxHeight = tempHeight > height3 ? tempHeight : height3;
System.out.println("这三个和尚中身高最高的是:" + maxHeight + "cm");
}
}
编译运行结果:
请输入第一个和尚的身高:
150
请输入第二个和尚的身高:
180
请输入第三个和尚的身高:
165
这三个和尚中身高最高的是:180cm
标签:JAVA,Scanner,int,System,sc,身高,数据,输入
来源: https://blog.csdn.net/ddddabb/article/details/113446306