编辑代码

import java.util.Scanner; 

public class ScannerDemo {
    public static void main(String[] args) {
        int[][] picture = new int[5][5];

        Scanner scan = new Scanner(System.in);

        // 1.input
        for (int i = 0; i < 5; i++) {
            // 从键盘接收数据
            // nextLine方式接收字符串
            System.out.println("当前在第 " + (i + 1) + " 行,请输入 5 个范围在 [0,255] 的整数:");
            // 判断是否还有输入
            if (scan.hasNextLine()) {
                String str = scan.nextLine();
                System.out.println(str);
                int count = 0;
                for (int start = 0; start < str.length(); start++) {
                    // System.out.println("str.length() = " + str.length() + " start = " + start);
                    int end = start + 1;
                    for (;end < str.length(); end++) {
                        if (str.substring(end, end+1).equals(" ")) {
                            break;
                        }
                    }
                    // System.out.println("start = " + start + ", end = " + end);
                    // System.out.println(str.substring(start, end));
                    picture[i][count] = Integer.valueOf(str.substring(start, end));
                    count++;
                    start = end;
                }
            }
        }
        scan.close();

        for (int[] row : picture) {
            for (int v : row) {
                System.out.print(v + " ");
            }
            System.out.println();
        }
        System.out.println();

        //2. process
        for (int r = 0; r < picture.length; r++) {
            for (int c = 0; c < picture[0].length; c++) {
                picture[r][c] = neighbourSum(picture, r, c);
            }
        }

        for (int[] row : picture) {
            for (int v : row) {
                System.out.print(v + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static int neighbourSum(int[][] picture, int r, int c) {
        int sum = 0;
        if (r == 0 && c == 0) {
            // System.out.println("top left");
            sum = picture[r][c + 1] + picture[r + 1][c] + picture[r + 1][c + 1];
            picture[r][c] = sum / 3;
        } else if (r == 0 && c == picture[0].length - 1) {
            // System.out.println("top right");
            sum = picture[r][c - 1] + picture[r + 1][c - 1] + picture[r + 1][c];
            picture[r][c] = sum / 3;
        } else if (r == picture.length - 1 && c == 0) {
            // System.out.println("bottom left");
            sum = picture[r - 1][c] + picture[r - 1][c + 1] + picture[r][c + 1];
            picture[r][c] = sum / 3;
        } else if (r == picture.length - 1 && c == picture[0].length - 1) {
            // System.out.println("bottom right");
            sum = picture[r - 1][c - 1] + picture[r - 1][c] + picture[r][c - 1];
            picture[r][c] = sum / 3;
        } else if (r == 0) {
            // System.out.println("top");
            sum = picture[r][c - 1] + picture[r][c + 1] + picture[r + 1][c - 1] + picture[r + 1][c] + picture[r + 1][c + 1];
            picture[r][c] = sum / 6;
        } else if (r == picture.length - 1) {
            // System.out.println("bottom");
            sum = picture[r - 1][c - 1] + picture[r - 1][c] + picture[r - 1][c + 1] + picture[r][c - 1] + picture[r][c + 1];
            picture[r][c] = sum / 6;
        } else if (c == 0) {
            // System.out.println("left");
            sum = picture[r - 1][c] + picture[r - 1][c + 1] + picture[r][c + 1] + picture[r + 1][c] + picture[r + 1][c + 1];
            picture[r][c] = sum / 6;
        } else if (c == picture[0].length - 1) {
            // System.out.println("right");
            sum = picture[r - 1][c - 1] + picture[r - 1][c] + picture[r][c - 1] + picture[r + 1][c - 1] + picture[r + 1][c];
            picture[r][c] = sum / 6;
        } else {
            // System.out.println("else");
            sum = picture[r - 1][c - 1] + picture[r - 1][c] + picture[r - 1][c + 1] + picture[r][c - 1] + picture[r][c + 1] + picture[r + 1][c - 1] + picture[r + 1][c] + picture[r + 1][c + 1];
            picture[r][c] = sum / 8;
        }
        return picture[r][c];
    }

}