建设一个商城网站,施工企业质量管理体系应按照我国,网站建设语,应用开发框架概述
使用方法求最大值。 创建方法求两个数的最大值max2#xff0c;随后再写一个求3个数的最大值函数max3。 要求#xff1a; 在max3这个方法中#xff0c;调用max2函数#xff0c;来实现3个数的最大值计算。
方法一
【代码】
public class P14 {public static int max…概述
使用方法求最大值。 创建方法求两个数的最大值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());}
}
【执行结果】