if else语句还有哪些替代方案?

时间:2017-11-08 03:06:12

标签: java

这是我程序中的一个方法,我不能为我的代码使用一堆if-else语句。我需要替代品。

    public String bedRoom1(){

        int newChoice10 = JOptionPane.showConfirmDialog(null,"Would "
        + "you like to explore this room?",
        "question",JOptionPane.YES_NO_OPTION);


        if (newChoice10 == JOptionPane.YES_OPTION){  //if the user would lieke to
    // explore then they are given the option for which objects they woul like to explore

            int newChoice4 = JOptionPane.showConfirmDialog(null,"Explore"
             + " the Rocking chair(type YES) or Window(type NO)?"
             , "question",JOptionPane.YES_NO_OPTION);

    //The following if else statments are still within
     //the first statement of the first if statement
            if (newChoice4 == JOptionPane.YES_OPTION){
                JOptionPane.showMessageDialog(null,"Chair starts "
               + "rocking with no one in it");

            }else{

                JOptionPane.showMessageDialog(null, "You see a child outs"
                  + "ide on a swing and he suddenly vanishes.");
            }
        }else if (newChoice10 == JOptionPane.NO_OPTION){
            JOptionPane.showMessageDialog(null,"You have chosen not "
         + "to explore Bedroom 1 and therefore continue "
         + "into the following room");

        }

        return null;



  }

请举例说明。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用showOptionDialog代替showConfirmDialog

进行所有选择

并使用switch - case代替if then else