package day08;
public class ChangeArgs_Exer {
public static void main(String[] arr){
// 求最大值
Count4 c = new Count4();
Object num;
int maxNum = c.max(5,3,9,7,15);
System.out.println("最大值:" + maxNum);
}
}
class Count4{
public int max(int num, int...other){ // 可变参数写在所有参数后面
// num = 5; other[] = {3,9,7}
// 默认第一个传递的数字 是最大值
int max = num;
for(int i = 0; i < other.length; i++){
// 暂时的最大值 挨个跟other中的数字进行比较
if(other[i] > max){
max = other[i];
}
}
return max;
}
求最大值
最新推荐文章于 2024-05-31 10:46:10 发布