gpt4 book ai didi

java - 幻方 NullPointerException。耶

转载 作者:行者123 更新时间:2023-11-29 06:06:26 26 4
gpt4 key购买 nike

<分区>

编辑:排序。解决方案见底部

好吧,我必须编写一个程序来接收用户输入,直到用户键入“x”,此时我的代码应该评估三件事:

  1. 如果输入数 (n) 是正方形
  2. 如果输入是唯一的并且包含数字 1-n
  3. 如果输入的顺序生成幻方。

我发布的所有内容都会编译,我可以在第一个输入中输入“x”或“(char)”,但如果我输入一个整数,我会得到:

Exception in thread "main" java.lang.NullPointerException at Square.add(Square.java:32) at TestMagicSquare.main(TestMagicSquare.java:40)

我做错了什么??这是我的代码。 (忽略总和检查方法,我还没有充实它们。)

功能类

    /*
* HW 01
* Nick Smith
* 1 December 2011
*
*/

import java.util.*;

public class Square
{
private ArrayList<Integer> numbers;
private int [][] square;
private int rowSum, colSum, TLBRSum, TRBLSum = 0;

/*
* Default constructor of Square class
*
*/
public Square ()
{

}

//step 0 add inputs to arraylist numbers
/*
*
*
*/
public void add(int i)
{
numbers.add(i);
}

//step 1 after inputs, number of inputs are square
/*
*
*
*/
public boolean isSquare()
{
for(int i=0; i<50; i++)
{
if(numbers.size() == i*i)
{
return true;
}
}
return false;
}

//step 2 after inputs, check if all unique #s are used
/*
*
*
*/
public boolean isUnique()
{
for(int i = 1; i<=numbers.size(); i++)
{
if(numbers.contains(i))
{
return true;
}
}
return false;
}

//step 3 populate indices in numbers into square

/*
*
*
*/
public void addToSquare()
{
for(int i=0; i<square.length; i++)
{
for(int j=0; j<square[0].length; j++)
{
square[i][j] = numbers.get(i*square.length+j);
}
}


}

//step 4 find the magic number

/*
*
*
*/
public int findMagicNumber()
{
int magicNumber = 0;

for(int i=0; i < square.length; i++)
{
magicNumber += square[0][i];
}

return magicNumber;
}

/*
*
*
*/
public int addRows()
{

return rowSum;
}

/*
*
*
*/
public int addCols()
{
return colSum;
}

/*
*
*
*/
public int addTLBR()
{
return TLBRSum;
}

/*
*
*
*/
public int addTRBL()
{
return TRBLSum;
}

//step 5, check if rows = cols = diagonals = magic number
/*
*
*
*/
public boolean isMagic()
{
if (addRows() == addCols() && addTLBR() == addTRBL() && addRows() == addTRBL() && addRows() == findMagicNumber())
{
return true;
}
return false;
}

测试类

    import java.util.*;

public class TestMagicSquare
{
public static void main (String [] args)
{

//instantiate new Scanner class object
Scanner scan = new Scanner(System.in);

//instantiate new Square class object from default constructor
Square mySquare = new Square();

//instance variables
boolean running = true;
String prompt = ". Enter a number (x to quit): ";
String error = "***Invalid data entry, please try again.***";
int inputNum = 1;

//Allow user to input until "x" is typed
while(running)
{
System.out.print("\nInput number " + inputNum + prompt);

if(!scan.hasNextInt())
{
if(scan.next().equalsIgnoreCase("x"))
{
System.out.println("Input terminated by user. Checking for square and magicness.");
running = false;
}
else
{
System.out.println(error);
}
}
else
{
inputNum++;
mySquare.add(scan.nextInt());
}
}

//Note for Dec 6 - Compiles, but not sure if this logic is right.

// Test inputs against constraints defined in functional class Square
if(!mySquare.isSquare())
{
System.out.println("Step 1: Number of inputs not square. Aborting program");
}
else if(!mySquare.isUnique())
{
System.out.println("Step 2: Numbers are not unique. Aborting program.");
}
else if(!mySquare.isMagic())
{
System.out.println("Step 3: Square is not magic. Aborting program.");
}
else
{
System.out.println("Step 1: Number of inputs is square");
System.out.println("Step 2: Numbers are unique");
System.out.println("Step 3: MAGIC SQUARE! Holy crap!");
}
}
}

一如既往,我们将不胜感激。

解决方案:

谢谢大家。我现在几乎明白了。非常感谢您的帮助。

我用过

    private ArrayList<Integer> numbers = new ArrayList();

在顶部声明中连同

    private int [][] square;

    double dubble = Math.sqrt(numbers.size());
int squareSize = (int)dubble;

square = new int[squareSize][squareSize];

在 AddToSquare() 方法中,我在测试类中的 while 循环之后直接调用了该方法。

我希望这对以后寻找答案的任何人有所帮助。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com