nxn 行列を作成し、その行列が魔方陣かどうかをチェックするプログラムを作成しました。私のメソッド magicCheck では、すべての要素が合計 (sum) され、n (行または列の数) で除算されて、magicNumber が生成されます。この magicNumber を使用して、各行、列、および対角線の合計を個別に比較して、それらが一致するかどうかを確認します。プログラムは正常に動作しますが、魔方陣であるべき数字を入力すると、プログラムは魔方陣ではないと言います。コードの順序に誤りがありますか、それとも数学で何かを見逃していますか?
import java.util.*;
public class MagicSquare
{
static int row, col, n, rows, columns, listSize;
static Scanner console = new Scanner (System.in);
public static void createArithmeticSeq(int [] list)
{
int first;
int diff;
//prompt user for first and diff to create list
System.out.println("Choose the first number and diff to create" +
" an arithmetic sequence.");
System.out.println("Enter first and diff : ");
first = console.nextInt();
diff = console.nextInt();
//process to create list of n*n elements
for (int i=0; i<listSize; i++)
{
list[i]=first+i*diff;
}
}
public static void matricize (int [] list, int [][] matrix)
{
System.out.println("Matricize arithmetic sequence:");
int i = 0;
//loop through each row
for (row=0; row<matrix.length; row++)
{
//loop through each column
for (col=0; col<matrix[row].length; col++)
{
//populate matrix with values from list
matrix[row][col] = list[i++];
}
}
}
public static void printMatrix(int [][] matrix)
{
for (row=0; row < matrix.length; row++)
{
for (col=0; col < matrix[row].length; col++)
System.out.printf("%2d" + " ", matrix[row][col]);
System.out.println("\n");
}
}
public static void reverseDiagonal(int [] [] matrix)
{
System.out.println("Reverse the diagonals:");
int temp;
for (row=0; row<matrix.length / 2; row++)
{
temp = matrix[row][row];
matrix[row][row] =
matrix[matrix.length - 1 - row] [matrix.length - 1 - row];
matrix[matrix.length - 1 - row][matrix.length - 1 - row] = temp;
}
for (row=0; row<matrix.length / 2; row++)
{
temp = matrix[row][matrix.length - 1 - row];
matrix[row][matrix.length - 1 - row] =
matrix[matrix.length - 1 - row][row];
matrix[matrix.length - 1 - row][row] = temp;
}
}
public static void magicCheck(int [] list, int [] [] matrix)
{
System.out.println("Is the matrix a magic square?");
int sum=0, sumRow=0, sumCol=0, sumDiag1=0, sumDiag2=0, magicNumber=0;
for(int i=0; i<listSize; i++)
{
sum = sum + list[i];
magicNumber = (sum/n);
for(row=0; row<matrix.length; row++)
{
//sum each row, then compare to magicNumber
for(col=0; col<matrix[row].length; col++)
sumRow = sumRow + matrix[row][col];
while (sumRow == magicNumber)
{
for(col=0; col<matrix.length; col++)
{
for(row=0; row<matrix[col].length; row++)
{
sumCol = sumCol + matrix[row][col];
while (sumCol == magicNumber)
{
for (int row=0; row<matrix.length; row++)
{
sumDiag1 += matrix[row][row];
}
while (sumDiag1 == magicNumber)
{
for (int row=n-1; row>=0; row--)
sumDiag2 += matrix[row][row];
while(sumDiag2 == magicNumber)
System.out.println("It is a magic square.");
System.out.println("The magic number is " + magicNumber + ".");
}
}
}
}
}
}
}
System.out.println("It is not a magic square.");
}
public static void main (String [] args)
{
//prompt user for array size
System.out.println("Enter size of array (in form nxn), n:");
n = console.nextInt();
rows = n;
columns = n;
listSize= (n*n);
int [] list = new int [listSize];
createArithmeticSeq (list);
int [] [] matrix = new int [rows] [columns];
matricize(list, matrix);
printMatrix(matrix);
System.out.print("\n");
reverseDiagonal(matrix);
printMatrix(matrix);
magicCheck(list, matrix);
}
}