package JianPanShuRu;
import java.util.Scanner;
/*
题目:键盘输入三个int数字,求其中的最大值
思路:
1. 尽然是键盘输入,肯定需要用到Scanner
2. Scanner三个步骤:导包、创建、使用nextInt()方法
3. 尽然是三个数字,那么要调用三个Scanner()方法,得到三个int变量
4. 无法同时判断三个数谁最大,应该转换成为两个步骤
4.1 首先判断前两个数当中谁最大,拿到前两个的最大值
4.2 拿着前两个中的最大值,再和第三个数字比较,到的三个数字当中到的最大值
*/
public class Demo03ScannerMax {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
System.out.println("键盘输入三个int数字,求其中的最大值");
System.out.print("请输入第一个数:");
int a = sr.nextInt();
System.out.print("请输入第二个数:");
int b = sr.nextInt();
System.out.print("请输入第三个数:");
int c = sr.nextInt();
//首先判断前两个数中的最大值
int max1 = a > b ? a : b;
//拿着前两个中的最大值,再和第三个数字比较
int max = max1 > c ? max1 : c ;
System.out.println("最大值是:" + max);
}
}
以上代码运行结果如下:
键盘输入三个int数字,求其中的最大值
请输入第一个数:20
请输入第二个数:10
请输入第三个数:30
最大值是:30