我的代码:
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”在私有布尔值处,甚至认为它就在if语句中。
您能帮我吗?谢谢!
**,如果您有任何改善建议,也请告诉我;)
您看到的错误是有效的,因为:
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循环结束时,您没有提供返回值。意思是,您的for循环运行,并且在第一次运行时返回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
}