2016-10-20 69 views
0

我是新來的人,對編程世界一般,所以請不要恨我。Java:警報窗口不會在「if」語句下打開

我的課剛開始使用「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

嘗試'medWindow.showAndWait() '。 – saka1029

+0

您應該使用調試器並逐步執行代碼。查看變量值並查看它爲什麼不落入if語句很容易。 學習如何使用調試器是程序員最有價值的工具之一。 – Nevets17

回答

2

你永遠做你的ifelse塊內部創建對話框東西。也許你刨去告訴他們......

此外!(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()