Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 程序未按预期运行_Java - Fatal编程技术网

Java 程序未按预期运行

Java 程序未按预期运行,java,Java,我正在做一个magicsquare程序,允许用户输入大于0的数字来形成magicsquare。什么是幻方,基本上是一个正方形,这意味着n必须有n个(平方)数。很像Tictatcoe,所有的行、列和对角线都有相同的和,在我运行程序时被视为幻方,它总是混淆2D数组集,并声称数字集是一个幻方,而通常情况下,不一定如此。请帮忙 import java.util.Scanner; public class SquareRunner { public static void main(String[]

我正在做一个magicsquare程序,允许用户输入大于0的数字来形成magicsquare。什么是幻方,基本上是一个正方形,这意味着n必须有n个(平方)数。很像Tictatcoe,所有的行、列和对角线都有相同的和,在我运行程序时被视为幻方,它总是混淆2D数组集,并声称数字集是一个幻方,而通常情况下,不一定如此。请帮忙

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;

    }
}
}
import java.util.Scanner;
公开课
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
平方测试=新平方();
System.out.println(“输入一行整数。完成后,在新行中键入“n”);
布尔标志=假;
while(!flag)
{
字符串编号=in.next();
if(数字等于(“n”))
flag=true;
其他的
测试。添加(数字);
}
test.isMagic();
}
}
公共课广场
{
私有int[][]值;
私人int row;
公众广场()
{
行=0;
}
公共无效添加(字符串编号)
{
int b=1;
int amount=numbers.length();
数值=新整数[金额][金额];

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

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

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

我有一种感觉……不过我不知道如何解决这个问题。你有什么想法吗?一个想法是在构造函数中分配
数组一次(传递
大小
参数),然后在
add()
方法中填充值。