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);
for (int i = 0; i < 5; i++) {
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++) {
int end = start + 1;
for (;end < str.length(); end++) {
if (str.substring(end, end+1).equals(" ")) {
break;
}
}
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();
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) {
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) {
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) {
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) {
sum = picture[r - 1][c - 1] + picture[r - 1][c] + picture[r][c - 1];
picture[r][c] = sum / 3;
} else if (r == 0) {
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) {
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) {
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) {
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 {
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];
}
}