这是魔方Java程序代码的逻辑错误吗?

这是魔方Java程序代码的逻辑错误吗?,java,arrays,loops,magic-square,Java,Arrays,Loops,Magic Square,我写了一个程序,创建一个nxn矩阵,然后检查矩阵是否是幻方。在我的方法magicCheck中,所有元素相加(求和),然后除以n(行数或列数),生成magicNumber。此magicNumber用于分别比较每行、每列和每对角线的总和,以查看它们是否匹配。程序运行正常,但当我输入的数字应该是幻方时,程序说不是。我的代码顺序有错误吗,或者我在数学上遗漏了什么 import java.util.*; public class MagicSquare { static int row, col, n

我写了一个程序,创建一个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);   
}

}
import java.util.*;
公共级魔法广场
{
静态int行、列、n、行、列、列表大小;
静态扫描仪控制台=新扫描仪(System.in);
公共静态void CreateArthmeticSeq(int[]列表)
{   
int优先;
int-diff;
//提示用户输入first和diff以创建列表
System.out.println(“选择要创建的第一个数字和差异”+
“算术序列。”);
System.out.println(“输入第一个和差异:”);
first=console.nextInt();
diff=console.nextInt();
//创建n*n个元素列表的过程

对于(int i=0;i这并不是一个真正的答案(对于评论来说有点长),但我建议您这样做:

// 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;
}
我肯定有人会对
Integer
取值为
null
有问题,但这只是一种方法

我不想为你重写你所有的代码,我相信你可以从那里找到答案


它将嵌套块保持在最小值,这使得代码更可读,更不容易出错。

给出一个输入/输出示例以及您希望它输出的内容。因此,没有大括号(col=0;col@Daniel还有什么不清楚的呢?这没什么好担心的。好吧,第一个问题是那些
while
循环。它们要么从未进入,要么从未退出。第二,对于
循环,嵌套的
太多了。我想你不必为这个问题超出两个嵌套级别。@Daniel当然,这个这里的缩进被破坏了,但这并不意味着正确的修复方法是放入更多的大括号。正确的修复方法是清楚地缩进内容。因为缩进不好,所以很容易出错,即使你将大括号放在任何可以选择的地方。谢谢。这是一个有用的建议。我对Java还是很陌生,所以我有时会做一些事情在某种程度上,这绝对是笨重和低效的。这是其中的一次。
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 + ".");
}