需求:设计一个方法用于获取数组中元素的最大值:
public class Test2 {
/*
需求:设计一个方法用于获取数组中元素的最大值
*/
public static void main(String[] args) {
// 定义一个数组,用静态初始化完成元素初始化
int[] arr = {11, 55, 22, 44, 33};
//调用获取最大值的方法,用变量接收返回值
int max = getMax(arr);
//把结果输出在控制台
System.out.println(max);
}
/*
定义一个方法,用来获取数组中的最大值
1、参数 int[] arr
2、返回值类型 int
*/
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
}
输出结果:
55