IntelliJ 变量可能尚未初始化
IntelliJ variable might not have been initialized
所以我想澄清的第一件事是,我认为这是我的错误,我只是没有看到,但我认为了解 IDE 是 IntelliJ 可能会有所帮助。此外,我一直在查看其他溢出帖子(question 1, final keyword, question 2 仅举几例),但据我所知他们还没有回答我的问题。我只学过AP计算机科学作为我在CS方面的教育,所以我的知识非常有限。
问题出在文件中的非 public class。它创建了一个通过二维数组的路径,它可以创建自己的实例以获得分支效果,但不是必须的。此外,该文件的多个版本将由文件中的 public class 创建。下面是我的代码(我认为相关的部分。如果您需要更多,请告诉我,我会更新我的问题)。如果您还有其他需要,请告诉我,谢谢您的帮助!
public class PathMaker
{
....
}
class RiverPath
{
private final int startID;
private final int endID;
private final double branchChance;
private final double endFactor;
public RiverPath(int x, int y, int[][] alts, double bChance, int c, double eFactor, int pX, int pY)
{
startID = MapNode.createID(x, y);
branchChance = bChance;
endFactor = eFactor;
...
endID = a number;
}
public RiverPath(int x, int y, int[][] alts, double bChance)
{
this(x, y, alts, bChance, 0, .02, -1, -1);
}
...
}
没有其他的构造函数,MapNode
是另一个class,带有public static int createID(int x, int y)
方法。
让我知道我需要做什么来使我的问题更清楚。
编辑:所有 4 个变量都让我感到悲伤。另外,我将把完整的构造函数放在下面。据我所知,我的代码中没有 return
语句。另外,错误是在我编译之前说
Variable [name] might not have been initialized
只有这 4 个错误。在构造函数之外定义了一些变量。
public RiverPath(int x, int y, int[][] alts, double bChance, int c, double eFactor, int pX, int pY)
{
count = c;
if(c > 0)
isBranch = true;
else
isBranch = false;//pX and pY only matter if is Branch
startID = MapNode.createID(x, y);
branchChance = bChance;
altitudes = alts;
endFactor = eFactor;
mainPath.add(new MapNode(x, y, altitudes));
boolean pathing = true;
MapNode currentNode = mainPath.get(0);
int[][] heights = new int[3][3];//heights around river
boolean[][] heightTight = new boolean[3][3];
int min;
int minCount;
int tID;
RiverPath branch;
while(pathing)
{
if(Math.random() < endFactor*count)
{
pathing = false;
}
else
{
count++;
min = 99;
minCount = 0;
for(int i = -1; i < 2; i++)
{//These loops fill heights with the nearby heights of mapnodes and set min as the min
for(int z = -1; z < 2; z++)
{
heights[i+1][z+1] = altitudes[currentNode.getY() + i][currentNode.getX() + z];
if(heights[i+1][z+1] < min)
{
min = heights[i + 1][z + 1];
}
}
}
if(min == currentNode.getAltitude())
{
min = 0;
for(int i = -1; i < 2; i++)
{
for(int z = -1; z < 2; z++)
{
tID = MapNode.createID(currentNode.getX() + z, currentNode.getY() + i);
if(heights[i+1][z+1] == currentNode.getAltitude() && (!isBranch || !(MapNode.createID(pX, pY) == tID)) && (mainPath.size() == 1 || mainPath.get(mainPath.size() - 1).getID() != tID))
{//if the altitude is the min, and it either isn't a branch or it isn't the node before this one
heightTight[currentNode.getY()+i][currentNode.getX()+z] = true;//a possible path exists here
min++;//min now keeps track of the total number of possible paths there are
minCount++;//also keeps track of total, but for a different implementation later
}
}
}
while(min != 0)
{
if(min == -1)
min = -2;//signals that we can test branches
for (int i = -1; i < 2; i++)
{
for (int z = -1; z < 2; z++)
{
if (min > 0 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < 1.0/)//
{
if(min > 0)
min = -1;//signals that we can skip all other true values, but ONLY if there are more possible branches
else
min = 0;//in case we lower min below 0
}
else if(min == -2 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < branchChance)//both random chance and it is a possible path
{
branch = new RiverPath(currentNode.getX() + z, currentNode.getY() + i, altitudes, branchChance, count, endFactor, currentNode.getX(), currentNode.getY());
branches.add(branch);
}
}
}
}
}
}
}
endID = currentNode.getID();
}
根据您分享的代码片段,我猜您在构造函数中的 ...
某处有一个条件 return
。由于你return早,endID
可能没有设置。这是导致此错误的常见原因。
编辑:
使用你发布的更大的代码片段,我可以在 IntelliJ 中重现你的问题,并且我在这一行看到了一个额外的错误 "Expression expected":
if (min > 0 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < 1.0 /)//
这(特别是 1.0
之后的尾随 /
)似乎是您的真正问题 - "might not have been initialized" 错误只是构造函数格式错误的症状。
所以我想澄清的第一件事是,我认为这是我的错误,我只是没有看到,但我认为了解 IDE 是 IntelliJ 可能会有所帮助。此外,我一直在查看其他溢出帖子(question 1, final keyword, question 2 仅举几例),但据我所知他们还没有回答我的问题。我只学过AP计算机科学作为我在CS方面的教育,所以我的知识非常有限。
问题出在文件中的非 public class。它创建了一个通过二维数组的路径,它可以创建自己的实例以获得分支效果,但不是必须的。此外,该文件的多个版本将由文件中的 public class 创建。下面是我的代码(我认为相关的部分。如果您需要更多,请告诉我,我会更新我的问题)。如果您还有其他需要,请告诉我,谢谢您的帮助!
public class PathMaker
{
....
}
class RiverPath
{
private final int startID;
private final int endID;
private final double branchChance;
private final double endFactor;
public RiverPath(int x, int y, int[][] alts, double bChance, int c, double eFactor, int pX, int pY)
{
startID = MapNode.createID(x, y);
branchChance = bChance;
endFactor = eFactor;
...
endID = a number;
}
public RiverPath(int x, int y, int[][] alts, double bChance)
{
this(x, y, alts, bChance, 0, .02, -1, -1);
}
...
}
没有其他的构造函数,MapNode
是另一个class,带有public static int createID(int x, int y)
方法。
让我知道我需要做什么来使我的问题更清楚。
编辑:所有 4 个变量都让我感到悲伤。另外,我将把完整的构造函数放在下面。据我所知,我的代码中没有 return
语句。另外,错误是在我编译之前说
Variable [name] might not have been initialized
只有这 4 个错误。在构造函数之外定义了一些变量。
public RiverPath(int x, int y, int[][] alts, double bChance, int c, double eFactor, int pX, int pY)
{
count = c;
if(c > 0)
isBranch = true;
else
isBranch = false;//pX and pY only matter if is Branch
startID = MapNode.createID(x, y);
branchChance = bChance;
altitudes = alts;
endFactor = eFactor;
mainPath.add(new MapNode(x, y, altitudes));
boolean pathing = true;
MapNode currentNode = mainPath.get(0);
int[][] heights = new int[3][3];//heights around river
boolean[][] heightTight = new boolean[3][3];
int min;
int minCount;
int tID;
RiverPath branch;
while(pathing)
{
if(Math.random() < endFactor*count)
{
pathing = false;
}
else
{
count++;
min = 99;
minCount = 0;
for(int i = -1; i < 2; i++)
{//These loops fill heights with the nearby heights of mapnodes and set min as the min
for(int z = -1; z < 2; z++)
{
heights[i+1][z+1] = altitudes[currentNode.getY() + i][currentNode.getX() + z];
if(heights[i+1][z+1] < min)
{
min = heights[i + 1][z + 1];
}
}
}
if(min == currentNode.getAltitude())
{
min = 0;
for(int i = -1; i < 2; i++)
{
for(int z = -1; z < 2; z++)
{
tID = MapNode.createID(currentNode.getX() + z, currentNode.getY() + i);
if(heights[i+1][z+1] == currentNode.getAltitude() && (!isBranch || !(MapNode.createID(pX, pY) == tID)) && (mainPath.size() == 1 || mainPath.get(mainPath.size() - 1).getID() != tID))
{//if the altitude is the min, and it either isn't a branch or it isn't the node before this one
heightTight[currentNode.getY()+i][currentNode.getX()+z] = true;//a possible path exists here
min++;//min now keeps track of the total number of possible paths there are
minCount++;//also keeps track of total, but for a different implementation later
}
}
}
while(min != 0)
{
if(min == -1)
min = -2;//signals that we can test branches
for (int i = -1; i < 2; i++)
{
for (int z = -1; z < 2; z++)
{
if (min > 0 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < 1.0/)//
{
if(min > 0)
min = -1;//signals that we can skip all other true values, but ONLY if there are more possible branches
else
min = 0;//in case we lower min below 0
}
else if(min == -2 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < branchChance)//both random chance and it is a possible path
{
branch = new RiverPath(currentNode.getX() + z, currentNode.getY() + i, altitudes, branchChance, count, endFactor, currentNode.getX(), currentNode.getY());
branches.add(branch);
}
}
}
}
}
}
}
endID = currentNode.getID();
}
根据您分享的代码片段,我猜您在构造函数中的 ...
某处有一个条件 return
。由于你return早,endID
可能没有设置。这是导致此错误的常见原因。
编辑:
使用你发布的更大的代码片段,我可以在 IntelliJ 中重现你的问题,并且我在这一行看到了一个额外的错误 "Expression expected":
if (min > 0 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < 1.0 /)//
这(特别是 1.0
之后的尾随 /
)似乎是您的真正问题 - "might not have been initialized" 错误只是构造函数格式错误的症状。