Java: ошибка недостижимого оператора

Для следующего кода, упомянутого ниже, я получаю сообщение об ошибке "Ошибка недостижимого оператора" в операторе " Return Cols "

Код вычисляет положение максимальной дозы в сгенерированном файле вывода 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 ответа

Решение

Вы уже отказались от кода, используя return rows;, Это утверждение возвращается звонящему. Итак, заявление после return rows; недоступен

Вы не можете сделать это.

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;       
}

Вы должны прочитать это.

После возврата код не обрабатывается далее, поэтому он выдает ошибку недостижимого кода, потому что вы возвращаете строки, и код выходит туда, и поэтому возвращаемые столбцы не будут достигнуты

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.
    }
Другие вопросы по тегам