【问题标题】:Java:Unreachable statement errorJava:无法访问的语句错误
【发布时间】:2014-09-18 09:14:29
【问题描述】:

对于下面提到的以下代码,我在“Return Cols”语句中得到了错误“Unreachable statement error”

代码计算最大剂量在生成的输出 CSV 文件中的位置

public int getPosition() {

        double dose = 0.0;
        double position = 0.0;
        int rows = 0;
        int cols = 0;

        String s;

        for (int j = 1; j < nz; j++) {
            s = "";
            for (int i = 1; i < nx; i++) {
                for (DetEl det_el : det_els) {
                    if (det_els.get(j + i * nz).getDose() == getMaxDose()) {
                        i=rows;
                        j=cols;
                    }
                    // comma separated or Semicolon separated mentioned here
                }
                // prints out the stream of  values in Doses table separated by Semicolon
            }
        }
        return rows;
        return cols;//unreachable statement error obtained at this position.
    }

非常感谢任何帮助

【问题讨论】:

  • 您的return rows 语句返回rows 并结束该方法(返回给调用者,使用提供的值)。如果要返回多个值,则需要返回一个对象,例如 int[] 甚至更好 - Pair
  • 你认为return rows; return cols;是什么意思?
  • 看起来您还想要 rows=icols=j 而不是 i=rowsj=cols。目前,rowscols 最后都为零,因为你从不写信给他们。

标签: java compiler-errors unreachable-code


【解决方案1】:

你不能这样做。

return rows; // when your program reach to this your program will return
return cols; // then never comes to here

如果你想从一个方法返回多个值,你可以使用Array或者你自己的Object

例如:

public int[] getPosition(){
  int[] arr=new int[2];
  arr[0]=rows;
  arr[1]=cols;
  return arr;       
}

您应该阅读this

【讨论】:

    【解决方案2】:

    您已经使用return rows; 打破了代码。该语句返回给调用者。所以,return rows; 之后的语句是不可访问的

    【讨论】:

      【解决方案3】:

      返回后代码没有进一步处理,这就是为什么它给出无法访问的代码错误,因为你正在返回行并且代码在那里退出,因此返回列将不会到达

      public int getPosition() {
      
              double dose = 0.0;
              double position = 0.0;
              int rows = 0;
              int cols = 0;
      
      
              String s;
      
      
              for (int j = 1; j < nz; j++) {
                  s = "";
      
                  for (int i = 1; i < nx; i++) {
      
                      for (DetEl det_el : det_els) {
      
                          if (det_els.get(j + i * nz).getDose() == getMaxDose()) {
      
      
                              i=rows;
                              j=cols;
      
      
      
      
      
                          }
                          // comma separated or Semicolon separated mentioned here
                      }
      
                      // prints out the stream of  values in Doses table separated by Semicolon
                  }
      
              }
              return rows;// code ends here itself thats why return cols is unreachable
      
              return cols;//unreachable statement error obtained at this position.
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-31
        • 1970-01-01
        • 2016-03-30
        • 2017-12-08
        • 2023-03-22
        • 1970-01-01
        • 2018-06-23
        相关资源
        最近更新 更多