【问题标题】:How to execute Yes,No Dialog boxes in a while loop?如何在while循环中执行是,否对话框?
【发布时间】:2021-03-26 03:07:17
【问题描述】:

所以,我必须使用 JOptionPane 对话框提供选择是或否的选项,如果用户选择否,程序将以消息“程序结束!”结束,如果用户单击是,则用户需要再次选择“是”或“否”,如果用户单击“否”2 或 3 次后,我必须使用该消息终止程序。 public static void main(String[] args) {

    int yesNo;
    String title = "Do you want to continue string comparision?";
    String task = "Strings Comparision?";
    String terminateTask = "End:\nThe program terminates!" + "\nEnd of this program.";

    yesNo = JOptionPane.showConfirmDialog(null, "Q: " + title, task, JOptionPane.YES_NO_OPTION);

    while (yesNo == JOptionPane.YES_OPTION) {

        JOptionPane.showConfirmDialog(null, "ReQ: " + title, task, JOptionPane.YES_NO_OPTION);
        System.out.println("Test: End of a single while-loop!");

        }
    if (yesNo == JOptionPane.NO_OPTION) {
        System.out.println(terminateTask);
        System.exit(0);
        
    }

    
}

}

【问题讨论】:

  • 欢迎来到 Stackoverflow。请通过显示预期结果以及您不满意的结果来扩展您的问题。可能显示包含错误消息的任何日志文件段落。最后但并非最不重要的一点是,向我们展示您失败的努力。

标签: java swing joptionpane


【解决方案1】:

您可以保留一个计数器变量,如果它是某个数字,则终止程序。我稍微修改了你的代码,所以你可以有这样的东西:

    int counter = 0;
    while (true) {
        yesNo = JOptionPane.showConfirmDialog(null, "Q: " + title, task, JOptionPane.YES_NO_OPTION);
        
        if (yesNo == JOptionPane.NO_OPTION ){
            System.out.println(terminateTask);
            System.exit(0);
        }
        if ((counter >= 2)){
            System.out.println("Test: End of a single while-loop!");
            System.exit(0);
        }
         counter++;

}

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多