我已经编写了此程序来创建一个列表,然后使用该列表填充数组。然后,方法magicCheck检查矩阵是否为幻方。我最初将其写为大小为4x4的已定义矩阵。当我这样做时,一切正常。现在,我想让用户确定数组(nxn)的大小。添加代码提示用户输入n,并基于nxn而不是4x4创建矩阵后,我的printMatrix方法已停止工作。

当我按原样运行程序并输入n = 2,first = 2,diff = 2时,这是我得到的输出:
“输入数组的大小(格式为nxn),n:
4
输入first和diff:
2
2

它不是魔术广场。”

谁能告诉我为什么printMatrix不再起作用?另外,我知道我的magicCheck方法的循环很草率,并且我确定有更好的方法来处理它,但是我仍然很新,可以将其拼凑起来以使其正常工作。请轻柔:]

这是我的代码:

import java.util.*;

public class MagicSquare
{
static int row, col, n;
static final int rows = n;
static final int columns = n;
static final int listSize = (n*2);
static Scanner console = new Scanner (System.in);

public static void createArithmeticSeq(int [] list)
{
    //prompt user for array size
    System.out.println("Enter size of array (in form nxn), n:");
    n = console.nextInt();
    int first;
    int diff;
    //prompt user for first and diff
    System.out.println("Enter first and diff : ");
    first = console.nextInt();
    diff  = console.nextInt();
    //process to create list of 16 elements
    for (int i=0; i<listSize; i++)
    {
        list[i]=first+i*diff;
    }
}

public static void matricize (int [] list, int [][] matrix)
{
    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)
{
    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)
{
    int sum=0, sumRow=0, sumCol=0, sumDiag1=0, sumDiag2=0, magicNumber=0;

    for(int i=0; i<listSize; i++)
    {
        sum += list[i];
        magicNumber = sum /= 4;

    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)
                    {
                    sumDiag1 = matrix[0][0]+matrix[1][1]+matrix[2][2]+matrix[3][3];
                        while (sumDiag1 == magicNumber)
                        {
                        sumDiag2 = matrix[3][0]+matrix[2][1]+matrix[1][2]
                        +matrix[0][3];
                            while(sumDiag2 == magicNumber)
                            System.out.println("It is a magic square.");
                        }
                    }
                }
            }
        }
    }
    }
            System.out.println("It is not a magic square.");

}

public static void main (String [] args)
{
    int [] list = new int [listSize];
    int [] [] matrix = new int [rows] [columns];
    createArithmeticSeq (list);
    matricize(list, matrix);
    printMatrix(matrix);
    System.out.print("\n");
    reverseDiagonal(matrix);
    printMatrix(matrix);
    magicCheck(list, matrix);
}

}

最佳答案

您声明:

static int row, col, n;
static final int rows = n;
static final int columns = n;
static final int listSize = (n*2);

根据用户输入设置n之前。
首先,从声明中删除final
其次,阅读后:
n = console.nextInt();

做:
rows = n;
columns = n;
...

最后,在您的main方法中,切换行的顺序:
int [] [] matrix = new int [rows] [columns];
createArithmeticSeq (list);

07-24 09:31