鍍金池/ 問答/Java/ 怎么不用if嵌套實(shí)現(xiàn)YES_NO_OPTION對(duì)話框的循環(huán)出現(xiàn)?

怎么不用if嵌套實(shí)現(xiàn)YES_NO_OPTION對(duì)話框的循環(huán)出現(xiàn)?

我想實(shí)現(xiàn)現(xiàn)出一個(gè)對(duì)話框,點(diǎn)擊yes按鈕后繼續(xù)跳出下一個(gè)對(duì)話框,點(diǎn)擊no按鈕跳出tips對(duì)話框提示點(diǎn)擊yes按鈕,按此循環(huán),現(xiàn)在我可以用if嵌套來實(shí)現(xiàn),我想請(qǐng)問有沒有更好的方法實(shí)現(xiàn)這個(gè),比如遞歸什么的?
部分代碼如下:

 //add yesbutton and yesbutton_actionlistener
        frame.add(yesButton);
        yesButton.addActionListener(e -> {
            int resbonse1 = JOptionPane.showOptionDialog(null, "questionOne", "infinityLove",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                    null, null, null);
            if (resbonse1 == JOptionPane.NO_OPTION) {
                JOptionPane.showOptionDialog(null, "please click yes", "tips",
                        JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
                        null, null, null);
            }

            //click yesbutton in the first dialog
            if (resbonse1 == JOptionPane.YES_OPTION) {
                int resbonse2 = JOptionPane.showOptionDialog(null, "questionTwo", "infinityLove",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                        null, null, null);
                if (resbonse2 == JOptionPane.NO_OPTION) {
                    JOptionPane.showOptionDialog(null, "please click yes", "tips",
                            JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
                            null, null, null);
                }

                //click yesbutton in the second dialog
                if (resbonse2 == JOptionPane.YES_OPTION) {
                ...
                }
            }
        }
回答
編輯回答
風(fēng)畔

狀態(tài)機(jī)模式了解一下

但是你這個(gè)沒有退出條件,要死循環(huán)嗎。

2017年7月30日 05:40