【发布时间】:2020-03-13 21:35:30
【问题描述】:
我的代码:
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 语句中。
你能帮帮我吗?谢谢!
*另外,如果您有任何改进的建议,请在评论中告诉我;)
【问题讨论】:
-
如果
cisla.length == 0,该方法返回什么? -
@AndyTurner 它永远不会是 0
-
@DownFury 编译器不知道这一点。您收到的错误是编译器错误,而不是在运行时。
-
@f1sh 但它设置 pocet 是 10 和 5 并且等于 cislo
-
@DownFury 重申:编译器不知道。编译器在很多方面都非常愚蠢(或者我应该说,很简单):它认为始终执行
for循环的唯一时间是条件是一个评估为真的常量表达式,例如true.
标签: java if-statement boolean private