数据输入
// 1.导入 java.util.Scanner 包
import java.util.Scanner;
public class Demo_1 {
public static void main(String[] args) {
/**
* 数据输入
**/
// 2.定义 Scanner 类型的变量,用于接收控制台输入。
Scanner scanner = new Scanner(System.in);
// 3.定义 int 类型的变量, 用于接收控制台输入的变量。
System.out.println("请输入一个整数 :");
int a = scanner.nextInt();
// 4.输出控制台输入的值 a 。
System.out.println("a : " + a);
}
}
三个和尚升级版
- 需求 :
一座寺庙里住着三个和尚,他们的身高必须经过测量得出,请用程序实现获取这三个和尚的最高身高 。
- 代码测试 :
//1.导入 java.util.Scanner 包
import java.util.Scanner;
public class Demo_1 {
public static void main(String[] args) {
/**
* 需求: 一座寺庙里住着三个和尚,他们的身高必须经过测量得出,请用程序实现获取这三个和尚的最高身高 。
* 分析 : 身高需要输入,定义 Scanner 用来接收 。
* 需要定义三个 int 类型的变量接收输入的值 。
* 用三元运算符输出较大值 。
**/
// 2. 定义 Scanner 变量,由于接收控制台输入。
Scanner scanner = new Scanner(System.in);
// 3. 定义 int 类型的变量用于接收控制台输入的三个和尚的身高
System.out.println("输入第一个和尚的身高(整数类型) :");
int height1 = scanner.nextInt();
System.out.println("输入第二个和尚的身高(整数类型) :");
int height2 = scanner.nextInt();
System.out.println("输入第三个和尚的身高(整数类型) :");
int height3 = scanner.nextInt();
// 4. 使用三元运算符输出最大值
int height = height1 > height2 ? height1 : height2;
int maxHeight = height > height3 ? height : height3;
// 5. 输出最大值
System.out.println("最高身高为 : " + maxHeight);
}
}