java在同一個類中定義多個方法(重載)


 1 import java.util.Scanner;
 2 
 3 public class test72 {
 4     /**
 5      * 求兩個數里的最大數
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         Scanner scan = new Scanner(System.in);
10         int a = scan.nextInt();
11         int b = scan.nextInt();
12         int max1 = fac1(a,b);
13         double c = scan.nextDouble();
14         double d = scan.nextDouble();
15         double max2 = fac1(c,d);
16         double e = scan.nextDouble();
17         double f = scan.nextDouble();
18         int g = scan.nextInt();
19         double max3 = fac1(e,f,g);
20         double min3 = fac2(e,f,g);
21 
22 
23 
24         System.out.println(max1);
25         System.out.println(max2);
26         System.out.println(max3);
27         System.out.println(min3);
28 
29     }
30 
31     public static int fac1(int a,int b) {
32         //求兩個整數里的最大數
33         return a>b ? a : b;
34     }
35     public static double fac1(double a,double b) {
36         //求兩個小數里的最大數
37         return a>b ? a : b;
38     }
39     public static double fac1(double a,double b,int c) {
40         //求兩個小數里和一個整數的大小關系
41         double max1 = a>b ? a : b;
42         double max2 = max1>c ? max1 : c;
43         return max2;
44     }
45     public static double fac2(double a,double b,int c) {
46         //求兩個小數里和一個整數的大小關系
47         double min1 = a<b ? a : b;
48         double min2 = min1<c ? min1 : c;
49         return min2;
50     }
51 }
 1 import java.util.Scanner;
 2 
 3 public class test72 {
 4     /**
 5      * 求兩個數里的最大數
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         Scanner scan = new Scanner(System.in);
10         int a = scan.nextInt();
11         int b = scan.nextInt();
12         int max1 = fac1(a,b);
13         double c = scan.nextDouble();
14         double d = scan.nextDouble();
15         double max2 = fac1(c,d);
16         double e = scan.nextDouble();
17         double f = scan.nextDouble();
18         int g = scan.nextInt();
19         double max3 = fac1(e,f,g);
20         double min3 = fac2(e,f,g);
21 
22 
23 
24         System.out.println(max1);
25         System.out.println(max2);
26         System.out.println(max3);
27         System.out.println(min3);
28 
29     }
30 
31     public static int fac1(int a,int b) {
32         //求兩個整數里的最大數
33         return a>b ? a : b;
34     }
35     public static double fac1(double a,double b) {
36         //求兩個小數里的最大數
37         return a>b ? a : b;
38     }
39     public static double fac1(double a,double b,int c) {
40         //求兩個小數里和一個整數的大小關系
41         double max1 = a>b ? a : b;
42         double max2 = max1>c ? max1 : c;
43         return max2;
44     }
45     public static double fac2(double a,double b,int c) {
46         //求兩個小數里和一個整數的大小關系
47         double min1 = a<b ? a : b;
48         double min2 = min1<c ? min1 : c;
49         return min2;
50     }
51 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM