【问题标题】:IntelliJ variable might not have been initializedIntelliJ 变量可能尚未初始化
【发布时间】: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 修复了编译错误(自然是使用虚拟数据),我看不出你的最终变量有任何问题。您需要清楚和简洁解释您的问题,并提供支持该问题的代码。没有它,我们就会在黑暗中冒险,这对所有相关人员来说都是一种浪费。
  • 我相信将这段代码分解成更小的部分是值得的。您面临的主要问题可能是您有一个大功能在做很多事情。这使得很难预测每一步需要哪些变量。除此之外,更容易跟踪所有变量和案例。具有一个目的的较小功能的另一个优点是它们更易于阅读(例如,供您的导师使用,以便以后记住您所做的事情)

标签: java final


【解决方案1】:

离开您共享的 sn-p,我猜您的构造函数中的 ... 某处有一个条件 return。由于您提前返回,endID 可能无法设置。这是导致此错误的常见原因。


编辑:

使用您发布的更大的代码 sn-p,我可以在 IntelliJ 中复制您的问题,并且我在此行看到一个额外的错误“预期表达式”:

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

这(特别是 1.0 之后的尾随 /)似乎是您的真正问题 - “可能尚未初始化”错误只是您的构造函数格式错误的症状。

【讨论】:

  • 如果立即编译问题得到解决,sn-p 将正常工作。仅从 OP 提供的内容尚不清楚是什么原因造成的,所以我们不要猜测。
  • 你是对的,我错过了除法标志。抱歉,我没有上传足够的代码开始,感谢您对我的包容!
  • 没问题,现在你知道了 :) 创建MCVE 始终是一个很好的调试步骤,因为它可以帮助您找出根本问题。然后,如果您自己无法确定解决方案,您可以使用现成的示例与他人分享。
猜你喜欢
  • 2012-03-25
  • 2015-07-04
  • 1970-01-01
  • 2013-11-24
相关资源
最近更新 更多