2017-07-03 234 views
-3

所以我首先要明确的是,我认为这是我的错误,我只是看不到,但我想它可以帮助知道IDE是IntelliJ。另外,我一直在寻找其他溢出帖子(question 1final keywordquestion 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); 
    } 
    ... 
} 

有没有其他构造,并且MapNodepublic 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(); 
} 
+4

共享[MCVE](https://stackoverflow.com/help/mcve)对于这类问题非常有帮助;如果您省略导致问题的部分代码,则很难诊断问题。 – dimo414

+0

哪个变量? –

+1

缺失:错误信息! – GhostCat

回答

-2

去关闭您分享的片段,我的猜测是,你在你的构造函数有一个条件return地方在...。由于您提早回来,因此可能不会设置endID。这是此错误的常见原因。


编辑:

有了较大的代码片断你贴我能够复制你的问题的IntelliJ,我看到一个额外的错误“表达式预期”在这条线:

if (min > 0 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < 1.0 /)// 

这(具体后1.0/)似乎成为你真正的问题 - “可能没有被初始化”的错误只是你的构造器格式错误的症状。

+0

如果解决了即时编译问题,代码片段将正常工作。目前还不清楚OP所提供的是什么原因,所以我们不要猜测。 – Makoto

+0

你说得对,我错过了分区标志。对不起,我没有上传足够的代码,并感谢您与我同在! –

+0

没问题,现在你知道了:)制作[MCVE](https://stackoverflow.com/help/mcve)总是一个很好的调试步骤,因为它可以帮助你识别根本问题。然后,如果您无法自行确定解决方案,则可以通过现成的示例与其他人分享。 – dimo414