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