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

Java if-else语句有哪些替代方案?,java,Java,这是我程序中的一种方法,我不能对我的代码使用一堆if-else语句。我需要其他选择 public String bedRoom1(){ int newChoice10 = JOptionPane.showConfirmDialog(null,"Would " + "you like to explore this room?", "question",JOptionPane.YES_NO_OPTION); if (ne

这是我程序中的一种方法,我不能对我的代码使用一堆
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;



  }

你能举个例子吗。提前感谢。

您可以在所有选择中使用
showOptionDialog
,而不是
showConfirmDialog


使用
switch-case
而不是
if-then-else

查找switch/case为什么不能使用if-else语句?它们对我很管用!