本人刚上手java小白,有错望各位大神及时批评指正。 package Martin; import java.util.Scanner; public class Martin03 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 定义数组的长、宽 System.out.println("请输入数组的行和列:"); int x = sc.nextInt(); int y = sc.nextInt(); // 定义数组 int[][] arr = new int[x][y]; // 对数组进行遍历赋值 System.out.println("请输入要赋入数组的数值"); for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) arr[i][j] = sc.nextInt(); } peak(arr); System.out.println("程序完成!"); } public static void peak(int[][] matix) { if (matix.length <= 2 && matix[0].length<=2) System.out.println("此数组全是边界没有局部最大值"); else { // 因为一个二维数组的最后一行和第一行都是边界所以可以直接跳过,第一列和最后一列同理 for(int row = 1; row < matix.length-1; row ++){ // row定义的是数组的行,j定义的数组的列 for(int j=1; j<matix[row].length-1; j++) { if (matix[row][j] >= matix[row][j-1] && matix[row][j] >= matix[row][j+1] && matix[row][j] >= matix[row-1][j] && matix[row][j] >= matix[row][j+1]) // 对局部最大值进行判断 System.out.println(matix[row][j]); } } } } }
以下是结果: