习题1(double数组最大值的查找)
定义方法max,实现求某个double数组的最大值,并返回
输入数组,并返回最大值
import java.util.Scanner;
public class Homework {
public static void main(String[] args) {
//作业一
A01 a1 =new A01();
Scanner myScanner = new Scanner(System.in);
double arr[] = new double[5];
System.out.print("请输入数组(5个数)");
for ( int i=0;i<5 ;i++ ) {
arr[i] = myScanner.nextDouble();
}
// double arr[]={1.3,23.1,12.0,78.35,24.4,3};
double max = a1.max(arr);
System.out.println("数组的最大值为" + max);
}
}
//作业一的类
class A01 {
//定义方法max,实现求某个double数组的最大值,并返回
public double max(double arr[]){
double max = arr[0];
for ( int i=0;i<arr.length ;i++ ) {
if (max<arr[i]) {
max=arr[i];
}
}
return max;
}
}