程序未按预期运行

时间:2012-02-12 18:33:08

标签: java

我正在做一个magicsquare程序,允许用户输入数字> 0以形成magicsquare。多么神奇的广场,几乎是一个正方形,意味着n必须有n个(平方)数字。就像ticTacToe一样,所有的行,列和对角线都有相同的总和被认为是一个魔术方格当我运行我的程序时,它总是混淆2D数组并声称这组数字通常是一个magicsquare,它不一定是这样的。请帮忙!

import java.util.Scanner;

public class SquareRunner
{
 public static void main(String[] args)
 {
     Scanner in = new Scanner(System.in);
     Square test = new Square();
     System.out.println("Enter a row of integers. When you are finished, type 'n' in a new line");
     boolean flag = false;
     while(!flag)
     {
        String numbers = in.next();
        if(numbers.equals("n"))
        flag = true;
        else
        test.add(numbers);
     }
     test.isMagic();

 }
}

public class Square
{
  private int[][] values;
  private int row;

public Square()
{
    row = 0;
}

public void add(String numbers)
{
  int b = 1;
  int amount = numbers.length();
  values = new int[amount][amount];

  for(int j =0;j<amount;j++)
  {
      String a = numbers.substring(j,b);
      int convert = Integer.parseInt(a);
      values[row][j] = convert;

      b++;
   }
    row++;

}


public Boolean isMagic()
{
    int checkAmountColumns = values[0].length;
    int checkAmountRows = values.length;
    int isSquare = checkAmountColumns * checkAmountRows;

        for(int q = 0;q<values.length;q++)
        {
            for(int w=0;w<values[0].length;w++)
            {
                int checkZero = values[q][w];
                if(checkZero == 0)
                {
                    System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect ");
                    System.out.println("Square i.e. 9 total numbers is 3 numbers per row");
                    return false;
                }
            }
        }
    if(checkAmountColumns != checkAmountRows || Math.sqrt(isSquare) != checkAmountColumns)
    {
        System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect ");
        System.out.println("Square i.e. 9 total numbers is 3 numbers per row");
        return false;
    }

  else
    {
        int magicNumber = 0;
        int counter = 0;
        int compareTo = 0;

       //row to row
        for(int i =0;i<values.length;i++)
        {
            for(int j = 0;j<values[0].length;j++)
            {

                values[i][j] += compareTo;
                if(counter == 0)
                values[i][j] += magicNumber;

            }
            counter ++;
            compareTo = 0;
            if(compareTo != magicNumber)
            {
                System.out.println("This Selection of numbers is not a perfect square");
                return false;
            }
        }

        //column to column
        for(int i =0;i<values[0].length;i++)
        {
             for(int j = 0;j<values.length;j++)
             {

                  values[j][i] += compareTo;
                  if(counter == 0)
                  values[j][i] += magicNumber;

              }
              counter ++;
              compareTo = 0;
              if(compareTo != magicNumber)
              {
                System.out.println("This Selection of numbers is not a perfect square");
                return false;
              }

        }
        System.out.println("This selection of numbers is a MagicSquare!");
        return true;

    }
}
}

2 个答案:

答案 0 :(得分:0)

我注意到的第一件事是你的add()方法可能无法正常工作。每次调用它时,都会用新分配的数组覆盖以前的values成员。这会丢弃您输入的上一行。

答案 1 :(得分:0)

与格雷格相同的答案。 将其添加到add方法并删除值数组的初始化。

if(values == null){values = new int[amount][amount];}