Mọi người coi hộ giúp e, bài này e làm ma trận xoắn ốc, nó chỉ đúng ở các trường hợp số lẻ như 3x3, 5x5, 7x7, 9x9 … còn các trường hợp khác nó đều báo lỗi ArrayIndexOutOfBoundsException
Nó báo ở dòng for thứ 2 trong while. Em cảm ơn ạ!
"C:\Program Files\Java\jdk-10.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.2.4\lib\idea_rt.jar=65140:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath E:\second\out\production\second java_array.bt4
Column: 6
Rows: 6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147483648
at java_array.bt4.main(bt4.java:25)
Process finished with exit code 1
package java_array;
import java.util.Scanner;
public class bt4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Column: ");
int column = scanner.nextInt();
System.out.print("Rows: ");
int row = scanner.nextInt();
int[][] arr = new int[row][column];
int value = row * column - 1;
int count = 0, temp, i, j;
while (value != 0) {
for (i = count; i < column - count; i++) {
arr[count][i] = value;
value--;
}
temp = i - 1;
for (j = count + 1; j < row - count; j++) {
arr[j][temp] = value;
value--;
}
temp = j - 1;
for (i = column - count - 2; i >= count; i--) {
arr[temp][i] = value;
value--;
}
temp = i + 1;
for (j = row - count - 2; j > count; j--) {
arr[j][temp] = value;
value--;
}
count++;
}
for (int[] x : arr) {
for (int y : x) {
System.out.printf("%5d", y);
}
System.out.println();
}
}
}


83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?