概述
使用方法求最大值。
创建方法求两个数的最大值max2,随后再写一个求3个数的最大值函数max3。
要求:
在max3这个方法中,调用max2函数,来实现3个数的最大值计算。
方法一
【代码】
public class P14 {
public static int max(int a,int b) {
return a > b ? a:b; //三目运算符
}
public static int max(int a,int b,int c) { //方法重载
int tmp = max(a,b);
return max(tmp,c);
}
public static void main(String[] args) {
// System.out.println(max3());
System.out.println(max(5,6,7));
}
}
【执行结果】

方法二
【代码】
public static int max2() {
int max = 0;
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
if (a > b){
max = a;
}else {
max = b;
}
return max;
}
public static int max3() {
int num = max2();
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
if (c > num) {
return c;
}else {
return num;
}
}
public static void main(String[] args) {
System.out.println(max3());
}
}
【执行结果】









![[山东大学操作系统课程设计]实验四+实验五](https://img-blog.csdnimg.cn/direct/e17445bb444140aa96f9f9d36886f034.png)









![[MySQL--进阶篇]存储引擎的体系结构、简介、特点、选择](https://img-blog.csdnimg.cn/direct/50cc2961e2644b7da434db26b72dfdef.gif#pic_center)