/*
求类数组中元素的最大值
*/
class Crecle1 {
private double radius;
private static double pi = 3.14;
Crecle1(double r) {
radius = r;
}
//求类数组中元素的值的大小
public static double compare(Crecle1[] arr) {
int max = 0; //定义最小的角标值
for(int i = 0; i < arr.length; i++) {
if(arr[i].radius > arr[max].radius) { //比较的类数组中参数的值。也就是比较的类半径的值
max = i; //把大值的角标交给max
}
}
return arr[max].radius;
}
}
class CrecleTest {
public static void main(String[] args) {
Crecle1[] c = new Crecle1[3];
c[0] = new Crecle1(2.0);
c[1] = new Crecle1(3.0);
c[2] = new Crecle1(4.0);
System.out.println("最大值是:" + Crecle1.compare(c));
}
} |
|