java中返回两个数的最大值

java中返回两个数的最大值
2025-05-08 12:08:29
推荐回答(5个)
回答1:

java返回两个数的最大值方法:

思路:if语句判断第一个数是否比第二个大,如果大则返回第一个,否则(第一个数小于等于第二个数)返回第二个数。

代码:

//方法返回值即为两个数中最大值
public int MaxNum(Num1,Num2){

//判断Num1是否大于Num2,是则返回Num1
if(Num1>Num2){
return Num1; 
} else {
//否则Num1<=Num2,返回Num2
return Num2;
}
}

回答2:

以下代码仅供参考

public class Main {
    public static void main(String args[]) {
        int num1 = 3;
        int num2 = 5;
        System.out.printf("两个数%d,%d 的最大值是:%d\n", num1, num2, maxNum(num1, num2));
    }

    private static int maxNum(int num1, int num2) {
        if (num1 > num2) {
            return num1;
        }
        return num2;
    }
}

回答3:

  1.   Math.max(1,2); 

   2.自己写个方法

回答4:

int a,b;
if(a>b){
return a;
}else{
return b;

}

回答5:

代码如下: