java定义方法,通过方法重载比较5.2,6.3,3,5,2.1,0.05,50的大小
时间: 2024-05-04 21:20:36 浏览: 13
以下是Java定义的方法,通过方法重载比较5.2,6.3,3,5,2.1,0.05,50的大小:
```java
public class Example {
public static void main(String[] args) {
double a = 5.2;
double b = 6.3;
int c = 3;
int d = 5;
double e = 2.1;
double f = 0.05;
int g = 50;
System.out.println("Max of " + a + " and " + b + " is " + max(a, b));
System.out.println("Max of " + c + " and " + d + " is " + max(c, d));
System.out.println("Max of " + e + " and " + f + " is " + max(e, f));
System.out.println("Max of " + g + " and " + a + " is " + max(g, a));
}
public static double max(double a, double b) {
return a > b ? a : b;
}
public static int max(int a, int b) {
return a > b ? a : b;
}
public static double max(double a, double b, double c) {
return max(max(a, b), c);
}
public static int max(int a, int b, int c) {
return max(max(a, b), c);
}
}
```
输出结果为:
```
Max of 5.2 and 6.3 is 6.3
Max of 3 and 5 is 5
Max of 2.1 and 0.05 is 2.1
Max of 50 and 5.2 is 50.0
```
因此,50最大。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)