代码之家  ›  专栏  ›  技术社区  ›  DevanDev

Java:无法访问语句错误

  •  0
  • DevanDev  · 技术社区  · 10 年前

    对于下面提到的以下代码,我在“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.
        }
    

    非常感谢您的帮助

    3 回复  |  直到 10 年前
        1
  •  3
  •   Ruchira Gayan Ranaweera    10 年前

    你不能这样做。

    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
  •  1
  •   Deval Khandelwal    10 年前

    您已经使用 return rows; 。此语句返回给调用者。所以,在 返回行; 无法访问

        3
  •  1
  •   kirti    10 年前

    在返回之后,代码没有继续执行,这就是为什么它在那里给出了不可访问的代码错误,因为您正在返回行,代码在那里退出,因此不会到达返回列

    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.
        }
    
    推荐文章