gpt4 book ai didi

Java:在 "if"语句下不会打开警报窗口

转载 作者:行者123 更新时间:2023-12-02 03:17:01 25 4
gpt4 key购买 nike

我是这里的新手,也是编程世界的新手,所以请不要恨我。

我的类(class)刚刚开始使用“if”语句,由于某种原因,我无法显示我的 javafx 警报框。也欢迎任何使我的代码更短的技巧。

public void start(Stage primarystage) {

TextInputDialog getDemNums = new TextInputDialog();
getDemNums.setTitle("How Many?");
getDemNums.setHeaderText("Input number of people desired: ");
Optional<String> sNumber = getDemNums.showAndWait();
int result = Integer.valueOf(sNumber.get());

if(result>=10){
int bigGroup = result/2;
TextInputDialog tenPlus = new TextInputDialog();
tenPlus.setTitle("Group Assignment");
tenPlus.setContentText("The group size is " + bigGroup + " people.");
} else if (result>=3 && result<10) {
int medGroup = result/3;
Alert medWindow = new Alert(AlertType.INFORMATION);
medWindow.setTitle("Group Assignment");
medWindow.setContentText("The group size is " + medGroup + " people.");
} else if(result<3) {
Alert tooSmall = new Alert(AlertType.INFORMATION);
tooSmall.setTitle("ERROR");
tooSmall.setContentText("The number of people has to be at least three");

最佳答案

您永远不会对 if 中创建的对话框执行任何操作或else block 。也许您打算向他们展示...

此外!(result >= 10)相当于 result < 10!(result >= 3)相当于 result < 3因此您不必再次测试这些条件:

Dialog dialog;
if(result>=10){
int bigGroup = result/2;
dialog = new TextInputDialog();
dialog.setTitle("Group Assignment");
dialog.setContentText("The group size is " + bigGroup + " people.");
} else if (result>=3) {
int medGroup = result/3;
dialog = new Alert(AlertType.INFORMATION);
dialog.setTitle("Group Assignment");
dialog.setContentText("The group size is " + medGroup + " people.");
} else {
dialog = new Alert(AlertType.INFORMATION);
dialog.setTitle("ERROR");
dialog.setContentText("The number of people has to be at least three");
}
dialog.show(); // or showAndWait()

关于Java:在 "if"语句下不会打开警报窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40144964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com