-1

我编写了一个创建 nxn 矩阵的程序,然后检查矩阵是否是幻方。在我的方法magicCheck 中,所有元素相加(总和),然后除以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);   
}

}
4

1 回答 1

1

这不是一个真正的答案(评论有点长),但我建议你这样做:

// returns 'null' if not a magic square, otherwise returns the magic number
public static Integer getMagicNumber(int[] list, int[][] matrix)
{
  ...
  for (int i = 0; i < listSize; i++)
  {
    ...
  }
  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];
    if (sumRow != magicNumber)
      return null;
  }
  for (col = 0; col < matrix.length; col++)
  {
    //sum each column, then compare to magicNumber
    ...
  }
  ...
  return magicNumber;
}

然后在main你做:

Integer magicNumber = getMagic(...);
if (magicNumber == null)
  System.out.println("It is not a magic square.");
else
{
  System.out.println("It is a magic square.");
  System.out.println("The magic number is " + magicNumber + ".");
}

我敢肯定有人会在Integer接受 的值时遇到问题null,但这只是做事的一种方式。

我不想为你重写你所有的代码,我相信你可以从那里弄清楚。

它将嵌套块保持在最低限度,这使得代码更具可读性和不易出错的代码。

于 2013-06-10T22:27:29.527 回答