Magic Square程序为行,列,对角线输出不正确的和值

时间:2018-02-23 11:53:05

标签: java magic-square

我用一个驱动程序和一个类编写了一个Magic Square程序。我唯一的问题是添加行,列和对角线。我为列创建了一个名为“col”的int常量,每次对整行进行求和时计数为1。因此,多维数组首先将[0,0],[0,1],[0,2]加到平方的任何大小,然后col将递增,变为[1,0],[ 1,1],[1,2]等。然而,在我的添加方法计算一行,进行打印,然后回到下一行,col被设置回0.有没有办法可以阻止它重置并保持增量?我认为这是最后一步。注意:到目前为止,我只将col放在行和列求和方法上,我只想让它首先用于那些。

注意:请确保在传输此处提供的文本时正确命名文本文件

提前谢谢大家。

// ****************************************************************
// MagicSquare.java
//
// Text below is to be filled by student. 
//
// ****************************************************************
import java.util.Scanner;
public class MagicSquare 
{ 
    int[][] square;
    public MagicSquare(int size)
    {
      square = new int[size][size];      
    }
    int col = 0;
//--------------------------------------
//return the sum of the values in the given row
//--------------------------------------

    private int sumMagicRow(int size,int col)
    {
      int sum = 0;
      for (int i=0;i<(size);i++)
      {
         sum += square[col][i];
      }
      col++;
      return sum;
    }
//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------

    private int sumMagicCol(int size, int col)
    {
      int sum = 0;
      for (int i=0;i<size;i++)
      {
         sum += square[col][i];
      }
      col++;
      return sum;
    }
//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------

    private int sumMagicDiagMain(int size)
    {
      int sum = 0;
      for (int i=0;i<size;i++)
      {
         sum += square[i][i];
      }
      return sum;
    }
//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------

    private int sumMagicDiagRev(int size)
    {
      int sum = 0;
      for (int i=0;i<size;i++)
      {
         sum += square[i][(size-1)-i];
      }
      return sum;
    }

//--------------------------------------
//return true if the square is magic (all rows, cols, and diags
// have same sum), false otherwise
//-------------------------------------- 
    public boolean isMagicSquare(int size)
    {
       boolean answer =false;
       if(sumMagicCol(size,col)==sumMagicRow(size,col) && sumMagicRow(size,col)==sumMagicDiagMain(size) && sumMagicDiagMain(size)==sumMagicDiagRev(size))
       {
         answer=true;
       }
       return answer;
    }
//--------------------------------------
//compute and display sums of square including row, column, main diagonal, and other diagonal
//--------------------------------------
    public void printMagicSquareSums(int size)
    {
      for(int i=0;i<size;i++)
      {
         System.out.println("Sum of row " + i + " is: " + sumMagicRow(size,col));
      }
      for(int i=0;i<size;i++)
      {
         System.out.println("Sum of column " + i + " is: " + sumMagicCol(size,col));
      }
      for(int i=0;i<size;i++)
      {
         System.out.println("Sum of row " + i + " is: " + sumMagicDiagMain(size));
      }
      for(int i=0;i<size;i++)
      {
         System.out.println("Sum of row " + i + " is: " + sumMagicDiagRev(size));
      }
    }

//--------------------------------------
//read info into the square from the input stream associated with
//the Scanner parameter
//--------------------------------------
    public void readSquare(Scanner scan)
    {
        for (int row = 0; row < square.length; row++)
           for (int col = 0; col < square.length; col++)
              square[row][col] = scan.nextInt();
    }
//--------------------------------------
//print the contents of the square, neatly formatted
//--------------------------------------
    public void printSquare(int size)
    {
      int column=0;
      for(int x=0;x<size;x++)
      {
         for(int i=0;i<size;i++)
         {
            System.out.printf("%3d ",square[x][i]);
         }
         System.out.println("");
      }

    }          
}

驱动程序:

// ****************************************************************
// MagicSquareTest.java
//
// Text below is to be filled by student. 
//
// ****************************************************************
import java.util.Scanner;
import java.io.IOException;
import java.io.File;

public class MagicSquareTest
{
     public static void main(String[] args) throws IOException
     {
      Scanner scan = new Scanner(new File("magicText.txt"));


      // make sure that the file magicData is in the current directory
      int count = 1; //count which square we're on
      int size = scan.nextInt(); //size of next square
      int mem = size;

      //Expecting -1 at bottom of input file
      while (size != -1)
      {

         //create a new Square of the given size 
         MagicSquare s = new MagicSquare(size);
         size=mem;

         //call its read method to read the values of the square
         System.out.println("\n***** Square " + count + " *****");
         s.readSquare(scan);

         //print the square
         s.printSquare(size);

         //print the square id
         //System.out.println(count);

         //print the sums
         s.printMagicSquareSums(size);

         //determine and print whether it is a magic square
         System.out.println(s.isMagicSquare(size));
         //get size of next square
         size = scan.nextInt();
         count++;
     }
   }
}

文本文件(读入数组)

3
8  1  6
3  5  7
4  9  2
7
30  39  48   1  10  19  28
38  47   7   9  18  27  29
46   6   8  17  26  35  37
5   14  16  25  34  36  45  
13  15  24  33  42  44   4
21  23  32  41  43   3  12
22  31  40  49   2  11  20 
4
48   9   6   39
27  18  21   36
15  30  33   24
12  45  42    3
3
6  2  7
1  5  3
2  9  4
4
3  16   2  13
6   9   7  12
10  5  11   8
15  4  14   1
5
17  24  15   8   1
23   5  16  14   7
 4   6  22  13  20
10  12   3  21  19
11  18   9   2  25
7
30  39  48   1  10  28  19
38  47   7   9  18  29  27
46   6   8  17  26  37  35
5   14  16  25  34  45  36  
13  15  24  33  42   4  44
21  23  32  41  43  12   3
22  31  40  49   2  20  11 
-1  

1 个答案:

答案 0 :(得分:0)

我发现你的代码非常奇怪,我认为你还有很多东西需要修复。但是针对您的具体问题:

jUnit

在这里你给方法col,它与类中的字段(它不是常数)col不同。在col ++中,你增加sumMagicRow(int size,int col )。如果要增加类实例值,则应使用 this.col ,它表示使用未在该特定范围(方法)中定义但在类级别上的col。

不幸的是,阅读你的代码我认为它不会解决问题,因为迟早你会希望col回到0;否则如果你继续添加它你将留下数组的边界。例如,如果你有一个大小为3的正方形,那么当你去行时,你会读到col1,col2,col3 col将会是3,你将尝试阅读第4行。最好重新考虑你的算法。有很多事情需要改进。

例如,如果你有一个1到N的循环(方形大小)不是一个循环足以用一些数学检查所有行/列/对角线?我相信它应该是;)