gpt4 book ai didi

java - 内部编译器错误?

转载 作者:行者123 更新时间:2023-11-29 04:37:23 25 4
gpt4 key购买 nike

目前这段代码还没有完成,我只是在编写随机生成一个 3x3 数组所需的方法,每个空格都有不同的单个数字。但是,我在第一行收到一个错误,上面写着 Internal compiler error: java.lang.NullPointerException at org.eclipse.jdt.internal.compiler.ast.ReferenceExpression.copy(ReferenceExpression.java:141)。我不太确定这个错误是什么意思,因为我只是一个初出茅庐的程序员。我该如何解决这个错误?

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

}

public static int sumRows(int[][] array) {
int totalRowOne;
int totalRowTwo;
int totalRowThree;

totalRowOne = array[0][0] + array[0][1] + array[0][2];
totalRowTwo = array[1][0] + array[1][1] + array[1][2];
totalRowThree = array[2][0] + array[2][1] + array[2][2];

if(totalRowOne == totalRowTwo && totalRowOne == totalRowThree) {
return totalRowOne;
}
else {
return -1;
}
}

public static int sumColumns(int[][] array) {
int totalColumnOne;
int totalColumnTwo;
int totalColumnThree;

totalColumnOne = array[0][0] + array[1][0] + array[2][0];
totalColumnTwo = array[0][1] + array[1][1] + array[2][0];
totalColumnThree = array[0][2] + array[1][2] + array[2][2];

if(totalColumnOne == totalColumnTwo && totalColumnOne == totalColumnThree) {
return totalColumnOne;
}
else {
return -1;
}
}

public static int sumDiagonals(int[][] array) {
int totalDiagonalOne;
int totalDiagonalTwo;

totalDiagonalOne = array[0][0] + array[1][1] + array[2][2];
totalDiagonalTwo = array[0][2] + array[1][1] + array[2][0];

if(totalDiagonalOne == totalDiagonalTwo) {
return totalDiagonalOne;
}
else {
return -1;
}
}

public static boolean isUnique(int[][] array, int num) {
if(num == array[0][0] || num == array[0][1] || num == array[0][2] ||
num == array[1][0] || num == array[1][1] || num == array[1][2] ||
num == array[2][0] || num == array[2][1] || num == array[2][2]) {
return true;
}
else {
return false;
}
}

public static void displayMagicSquare(int[][] array) {
int counter1;
int counter2;

for(counter1 = 0; counter1 < 3; counter1++) {
for(counter2 = 0; counter2 < 3; counter2++) {
System.out.print(array[counter1][counter2]);
}
System.out.print("/n");
}
}

public static int[][] fillMatrix(int[][] array) {
int numberOne;
int numberTwo;
int numberThree;
int numberFour;
int numberFive;
int numberSix;
int numberSeven;
int numberEight;
int numberNine;

array[0][0] = 0;
array[0][1] = 0;
array[0][2] = 0;
array[1][0] = 0;
array[1][1] = 0;
array[1][2] = 0;
array[2][0] = 0;
array[2][1] = 0;
array[2][2] = 0;

numberOne = (int)(Math.random() * 8 + 1);
do {
numberTwo = (int)(Math.random() * 8 + 1);
} while(numberTwo == numberOne);

do {
numberThree = (int)(Math.random() * 8 + 1);
} while(numberThree == numberTwo || numberThree == numberOne);

do {
numberFour = (int)(Math.random() * 8 + 1);
} while(numberFour == numberThree || numberFour == numberTwo || numberFour == numberOne);

do {
numberFive = (int)(Math.random() * 8 + 1);
} while (numberFive == numberFour || numberFive == numberThree || numberFive == numberTwo ||
numberFive == numberOne);

do {
numberSix = (int)(Math.random() * 8 + 1);
} while(numberSix == numberFive || numberSix == numberFour || numberSix == numberThree ||
numberSix == numberTwo ||numberSix == numberOne);

do {
numberSeven = (int)(Math.random() * 8 + 1);
} while(numberSeven == numberSix || numberSeven == numberFive || numberSeven == numberFour ||
numberSeven == numberThree ||numberSeven == numberTwo ||numberSeven == numberOne);

do {
numberEight = (int)(Math.random() * 8 + 1);
} while(numberEight == numberSeven || numberEight == numberSix || numberEight == numberFive ||
numberEight == numberFour ||numberEight == numberThree ||numberEight == numberTwo ||
numberEight == numberOne);

do {
numberNine = (int)(Math.random() * 8 + 1);
} while(numberNine == numberEight || numberNine == numberSeven || numberNine == numberSix ||
numberNine == numberFive || numberNine == numberFour ||numberNine == numberThree ||
numberNine == numberTwo || numberNine == numberOne);

array[0][0] = numberOne;
array[0][1] = numberTwo;
array[0][2] = numberThree;
array[1][0] = numberFour;
array[1][1] = numberFive;
array[1][2] = numberSix;
array[2][0] = numberSeven;
array[2][1] = numberEight;
array[2][2] = numberNine;

return array;
}

public static boolean isMagicSquare(int[][] array) {
if(sumRows(int[][] array) == sumColumns(int[][] array) && sumColumns(int[][] array) == sumDiagonals(int[][] array)) {
return true;
}
else {
return false;
}
}

}

最佳答案

Internal compiler error: java.lang.NullPointerException at org.eclipse.jdt.internal.compiler.ast.ReferenceExpression.copy(ReferenceExpression.java:141)

意味着 Eclipse Java 编译器的实际内部结构非常困惑,以至于他们取消引用了一个空指针。根据定义,这是 Eclipse 中的错误。最有可能的是,由于代码中的某些错误,您遇到了这个错误;如果一个小的、正确的 Java 类命中其中之一,那将是非常令人惊讶的。

您可以尝试使用真正的 JDK 编译您的代码,它可能会告诉您需要修复的地方。一般来说,您不应该依赖 IDE 作为您唯一的构建工具;有一天你会想要从命令行进行可重复的构建,你会想要学习 ant、Maven 或 gradle。也许各种编译网站之一可以方便地满足您的即时需求。

关于java - 内部编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40747767/

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