public class ChangeArgs_Exer01 {
public static void main(String[] args) {
//最大值
Count4 c = new Count4();
int maxNum = c.max(5,3,9,7);
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;
}
}
Java基础:用类方法实现最大值
最新推荐文章于 2024-10-23 19:37:27 发布
本文介绍了一个使用Java编写的简单程序,该程序定义了一个名为Count4的类,其中包含一个用于找出一组整数中最大值的方法。通过实例化Count4类并调用其max方法,可以传入多个整数参数,并返回这些参数中的最大值。

2435

被折叠的 条评论
为什么被折叠?



