【发布时间】:2017-07-03 03:37:34
【问题描述】:
所以我要明确的第一件事是,我认为这是我没有看到的错误,但我认为知道 IDE 是 IntelliJ 可能会有所帮助。另外,我一直在查看其他溢出帖子(question 1、final keyword、question 2 等等),但据我所知,他们还没有回答我的问题。我的 CS 教育只学过 AP 计算机科学,所以我的知识非常有限。
问题在于文件中的非公共类。它通过 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 语句。另外,错误是在我编译之前说的
变量 [name] 可能尚未初始化
只有这 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 对这类问题非常有帮助;如果您省略了导致问题的部分代码,则很难诊断问题。
-
哪个变量??
-
缺少:错误信息!
-
如果我真的用你的代码 sn-p 修复了编译错误(自然是使用虚拟数据),我看不出你的最终变量有任何问题。您需要清楚和简洁解释您的问题,并提供支持该问题的代码。没有它,我们就会在黑暗中冒险,这对所有相关人员来说都是一种浪费。
-
我相信将这段代码分解成更小的部分是值得的。您面临的主要问题可能是您有一个大功能在做很多事情。这使得很难预测每一步需要哪些变量。除此之外,更容易跟踪所有变量和案例。具有一个目的的较小功能的另一个优点是它们更易于阅读(例如,供您的导师使用,以便以后记住您所做的事情)