public class ChangeArgs_Exer002 {
public static void main(String[] args) {
//求最大值
Count002 q = new Count002();
int maxNum = q.max(5,3,9,6,10);
System.out.println("最大值为:"+maxNum);
}
}
class Count002{
public int max(int num,int...other){
//可变参数写在所有参数后面
//num=5; other[]={3,9,6,10}
//默认第一个传递的数字 是最大值
int max =num;
for (int i=0;i<other.length;i++){
//暂时的最大值 挨个跟other中的数字进行比较
if (other[i]>max){
max=other[i];
}
}
return max;
}
}
JAVA_用类方法求最大值
于 2022-09-25 13:52:43 首次发布