创建幻方 (Java)

Creating a magic square (Java)

我有点不知道如何处理这个方阵编码项目。

每当我尝试输入任何值时,结果总是为真,方阵是一个幻方。例如,结果会是这样:

16 03 02 13
05 10 11 08
09 06 07 12
04 15 14 01

但是当我输入如下值时:

03 04 16 02
05 01 02 10
05 08 07 12
03 14 13 09

这应该 return 错误,但它仍然 return 正确地说它是一个幻方。

要求是我需要所有的方法

"public void add(int i, int row, int col)": 在矩阵的指定位置添加一个整数。

public

public boolean allInRange:判断矩阵中的所有值是否都在合适的范围内

public boolean allUnique:判断矩阵中的所有值是否只出现一次

"public boolean isMagic":判断矩阵是否说明幻方。这意味着:

这是我的driverclass

 import javax.swing.*;


public class SquareMatrixDriver { 
  
  public static void main(String[] args) { //My favorite line in history  
    
    
    JFrame bot = new JFrame(); //We can use JFrame to read the inputs 
    
    do 
    {
      //We have to make sure that it is a valid input or else I am doomed
      int size = 0;
      do 
      {
        size = Integer.parseInt(JOptionPane.showInputDialog(bot, "Enter the size of the matrix."));
        if (size < 1) 
        {
          JOptionPane.showMessageDialog(bot, "Invalid size! Enter a number greater than 0.");
        }
      } 
      while(size < 1);
      
      SquareMatrix matrix = new SquareMatrix(size);
      
      for (int i=0; i<size; i++) 
      {
        //Gets thhe User's Input
        
        String[] stringInput;
        do 
        {
          stringInput = JOptionPane.showInputDialog(bot, "Enter the row number" + (i + 1) + ", with " + size + " elements, split by commas.").split(",");
          if (stringInput.length != size) 
          { //In this code we basically enter the numbers with commas
            JOptionPane.showMessageDialog(bot, "Invalid size! " + stringInput.length + " elements entered but " + size + " required.");
          }
        } 
        while(stringInput.length != size);
        
        int[] intInput = new int[size];
        for (int o=0; o<size; o++) 
        { 
          
        }
        
        for (int o=0; o<size; o++) 
        {
          matrix.add(Integer.parseInt(stringInput[o]), i, o); //Here we would put everything into the Matrix
        }
      }
      
      
      JOptionPane.showMessageDialog(bot, "The matrix is " + (matrix.isMagic()? "very" : "not") + " correct"); //This line will output if the Matrix works or doesnt work 
      
      JOptionPane.showMessageDialog(bot, matrix); // Enters out the final output
    } while (JOptionPane.showConfirmDialog(bot, "Do you wish to exit?", "Exit", JOptionPane.YES_NO_OPTION) == 1); //Asks the User if they would like to exit the program
  }
}






      
      
     

我可以通过目测发现的错误:

allUnique() 是完全错误的,你必须检查每个数字是否只在矩阵中出现一次,但是你正在比较完全不同的行数组,检查唯一性的最好方法通常是使用hashset,但是因为这里你有一个非常定义的数字范围(从 1 到 n2),然后使用 n2 布尔值的任何数组。扫描方阵和test/set数组中对应的元素,如果已经设置returnfalse.

public boolean allUnique() {
  int n=array.length;
  boolean[] set=new boolean[n*n];
  for (int i=0; i < n; i++) {
    for (int j=0;j < n; j++) {
      //Here assuming that you already sucessfully called allInRange, 
      //otherwise we must check bounds to avoid an index out of bounds exception
      if(set[array[i][j]-1]) {
        return false;
      }
      set[array[i][j]-1] = true;
    }
  }
  return true;
}

在 isMagic() 方法中,这部分都是错误和多余的

 boolean range = allInRange();
  if (range == true)
    return true;  //WRONG, this will immediately return with true, without further checks
  if (range == false)
    return false;

  boolean unique = allUnique();
  if (unique == true)
    return true;  //WRONG, same as before
  if (unique == false)
    return false;

直接替换为

if (!allInRange()) {
    return false;
}
if (!allUnique()) {
    return false;
}

最后在 isMagic() 中计算列的总和时,缺少加法

sumCol = array[j][i];

必须替换为

sumCol += array[j][i];