所以我首先要明確的是,我認爲這是我的錯誤,我只是看不到,但我想它可以幫助知道IDE是IntelliJ。另外,我一直在尋找其他溢出帖子(question 1,final keyword,question 2僅舉幾例),但他們還沒有回答我的問題,據我所知。我只有AP計算機科學作爲我的CS教育,所以我的知識非常有限。IntelliJ變量可能尚未初始化
問題出在文件中的非公共類。它創建一個通過2d數組的路徑,它可以爲分支效果創建它自己的實例,但不一定。此外,該文件中的公共類將創建多個版本。以下是我的代碼(我認爲相關的部分,如果您需要更多,請告訴我,我會更新我的問題)。讓我知道您是否還有其他需要的東西,並感謝您的幫助!
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
與public static int createID(int x, int y)
方法另一個類。
讓我知道我需要做的事情,使我的問題更清晰。
編輯:所有4個變量是給我的悲傷。另外,我將把完整的構造函數放在下面。據我所見,我的代碼中沒有return
聲明。此外,錯誤的是我編譯之前,說
變量[名]可能尚未初始化
這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();
}
共享[MCVE](https://stackoverflow.com/help/mcve)對於這類問題非常有幫助;如果您省略導致問題的部分代碼,則很難診斷問題。 – dimo414
哪個變量? –
缺失:錯誤信息! – GhostCat