2017-07-03 119 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