私有布尔值无法识别 "if statement" 中的 return

Private boolean cannot recognize return in "if statement"

我的代码:


import javafx.event.ActionEvent;
import javafx.scene.control.Label;

import java.util.Arrays;
import java.util.Random;

public class Controller {
    public Label keno;
    public Label loto;
    public Random rd = new Random();

    public void click(ActionEvent actionEvent) {

        String pom = "";
        int[] ken = zrebuj(10, 80);
        pom = Arrays.toString(ken);
        pom = pom.substring(1, pom.length() - 1);
        keno.setText(pom);


        ken = zrebuj(5, 35);
        pom = Arrays.toString(ken);
        pom = pom.substring(1, pom.length() - 1);
        loto.setText(pom);
    }

    private int[] zrebuj(int pocet, int max) {

        int[] cisla = new int[pocet];

        for (int i = 0; i < cisla.length; i++) {
            int tah = rd.nextInt(max) + 1;

            if (jeTam(cisla, tah)==false) {
                cisla[i] = tah;
            }
            else i--;
        }
        Arrays.sort(cisla);
        return cisla;
    }

    private boolean jeTam(int[] cisla, int tah) {

        for (int i = 0; i < cisla.length; i++) {

            if (cisla[i] == tah) {
                return true;
            }
            else return false;
        }

    }
} 

我的问题是,当我启动程序时,它崩溃了,它在私有布尔值处显示:"Missing return statement",甚至认为它就在那里声明。

你能帮帮我吗?谢谢!

*此外,如果您有任何改进的建议,请在评论中告诉我;)

您看到的错误是有效的,因为:

private boolean jeTam(int[] cisla, int tah) {

    for (int i = 0; i < cisla.length; i++) {

        if (cisla[i] == tah) {
            return true;
        }
        else return false;
    }
  // <---- NO RETURN STATEMENT
}

您没有在 for 循环结束时提供 return 值。 意思是,你的 for 循环 运行s 和第一个 运行 returns false 或 true(根据你的逻辑)。

但是编译器会查看您的代码,发现方法末尾缺少 return 语句。

private boolean jeTam(int[] cisla, int tah) {

    for (int i = 0; i < cisla.length; i++) {

        if (cisla[i] == tah) {
            return true;
        }
        else return false;
    }

   return false //For Example

}