我是新来的,一般是编程世界所以请不要讨厌我。
我的班级刚开始使用“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");
答案 0 :(得分:2)
您永远不会对在if
或else
块内创建的对话框执行任何操作。可能你打算给他们看......
此外!(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()