使用JOption窗格的If和Else陈述

时间:2018-10-19 14:29:26

标签: java if-statement

你好,自从我开始学习Java已有大约一个月了。我正在尝试制作一个迷你万圣节迷你游戏,用户可以进入房屋并选择进行探索。我正在尝试通过if和else语句以及JOption窗格来做到这一点。问题似乎是即使我已重命名窗格并尝试访问其他窗格,也会始终触发JOptionPane.YES_OPTION。任何建议将不胜感激。

下面是代码

private static int DiningRoom;
private static int livingroom;
private static int upStairs;





public static void main(String[] args) {
   JFormattedTextField nameField = new JFormattedTextField();
   ImageIcon iconic = new ImageIcon("HauntedHousemap.png");


   /* New JPanel */
  JPanel myPanel = new JPanel();
  myPanel.add(new JLabel("Enter your name to play: "));
  myPanel.add(nameField);
  myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

  int result = JOptionPane.showConfirmDialog(null, myPanel, 
           "Enter Name", JOptionPane.OK_CANCEL_OPTION);
  if (result == JOptionPane.OK_OPTION) {   
  }

  JOptionPane.showMessageDialog(null,"Here is the Map!","Map",JOptionPane.INFORMATION_MESSAGE, iconic);

  JOptionPane livingRoomPane = new JOptionPane();
  JOptionPane startingPane = new JOptionPane();

  //***************************** STARTING OPTIONS **************************************//
  Object [] mainOptions = { "Living Room", "Dining Room", "Stairs" };
  Object [] StairsOptions = { "Bedroom1", "Beedroom2", "MasterBedroom"};
  Object [] DiningOptions = { "Kitchen", "Explore Items"};
  Object [] PantryOptions = { "Pantry", "Explore Items"};
  Object [] LivingOptions = { "Bathroom", "Explore Items"};
  Object [] Bathroom1stFloorOptions = { "Mirror", "Shower"};
  Object [] LivingRoomItems = { "Chest", "TV"};
  Object [] DiningRoomItems = { "Candelabra", "Table"};
  Object [] BedRoom1Items = { "Bathroom", "Explore Items"};
  Object [] BedRoom2Items = { "Bathroom", "Explore Items"};
  Object [] MasterBedRoomItems = { "Master Bathroom", "Explore Items"};



  //*********************************** User will choose where he wants to go from the front door **********************************//

  int startInput = startingPane.showOptionDialog(null, //Component parentComponent
                       ("Greetings " +  nameField.getText() + ", You are at the front door pick a path if you dare!"), //Object message,
                        "Haunted House", //String title
                       startingPane.YES_NO_OPTION, //int optionType
                       startingPane.INFORMATION_MESSAGE,  //int messageType
                       null,mainOptions, mainOptions [0]  ); //Icon icon, 

    //********************* IF PLAYER PICKED LIVING ROOM OUT OF STARTING OPTIONS ***************************************//
    switch (startInput) {
        case JOptionPane.YES_OPTION:
            System.out.println ("You are now in the living room");
            int livingRoom = JOptionPane.showOptionDialog(null, //Component parentComponent
                    ("Do you wish to go to the bathroom or explore the living room?"), //Object message,
                    "Haunted House", //String title
                    JOptionPane.YES_NO_OPTION, //int optionType
                    JOptionPane.INFORMATION_MESSAGE,  //int messageType
                    null,LivingOptions, LivingOptions [0]  );
            break;
        case JOptionPane.NO_OPTION:
            System.out.println ("You are now in the dinning room");
            int DiningRoom = JOptionPane.showOptionDialog(null, //Component parentComponent
                    ("Do you wish to go to the kitchen or explore the dining room?"), //Object message,
                    "Haunted House", //String title
                    JOptionPane.YES_NO_OPTION, //int optionType
                    JOptionPane.INFORMATION_MESSAGE,  //int messageType
                    null,DiningOptions, DiningOptions [0]  );
            break;
        default:
            System.out.println("You are now upstairs");
            int upStairs = JOptionPane.showOptionDialog(null, //Component parentComponent
                    ("Do you wish to go to Bedroom1, Bedroom2, or the Master Bedroom?"), //Object message,
                    "Haunted House", //String title
                    JOptionPane.YES_NO_OPTION, //int optionType
                    JOptionPane.INFORMATION_MESSAGE,  //int messageType
                    null,StairsOptions, StairsOptions [0]  );
            break; }




switch (livingroom) {
case JOptionPane.YES_OPTION: 
System.out.println ("You are now in the 1st floor Bathroom");
int bathRoomFloor1 = JOptionPane.showOptionDialog(null, 
               ("Do you wish to go to the bathroom or explore the living room?"), //Object message,
               "Haunted House", //String title
               JOptionPane.YES_NO_OPTION, //int optionType
               JOptionPane.INFORMATION_MESSAGE,  //int messageType
               null,Bathroom1stFloorOptions, Bathroom1stFloorOptions [0]  );
break;

default:
System.out.println ("You are now in the Exploring the living room");
int livingRoomExplore = JOptionPane.showOptionDialog(null, 
               ("Pick an Item to explore"), 
               "Haunted House", //String title
               JOptionPane.YES_NO_OPTION, //int optionType
               JOptionPane.INFORMATION_MESSAGE,  //int messageType
               null,LivingRoomItems, LivingRoomItems [0]  ); } 


switch (DiningRoom) {     
case JOptionPane.YES_OPTION: 
System.out.println ("You are now in the kitchen");
int kitchenFloor = JOptionPane.showOptionDialog(null, 
               ("Do you wish to go to the Pantry or explore the dining room?"),
               "Haunted House", 
               JOptionPane.YES_NO_OPTION, 
               JOptionPane.INFORMATION_MESSAGE,  
               null,PantryOptions, PantryOptions [0]  );
 break;
 default: 
 System.out.println ("You are now in the Exploring the dining room");
 int DiningRoomExplore = JOptionPane.showOptionDialog(null, 
               ("Pick an Item to explore"), 
               "Haunted House", 
               JOptionPane.YES_NO_OPTION, 
               JOptionPane.INFORMATION_MESSAGE,  
               null,DiningRoomItems, DiningRoomItems [0]  ); }

  switch (upStairs) {
  case JOptionPane.YES_OPTION: 
  System.out.println ("You are now in bedRoom1");
  int bedRoom1Explore = JOptionPane.showOptionDialog(null, 
               ("Do you want to go to the Bathroom or Explore the room?"), 
               "Haunted House", 
               JOptionPane.YES_NO_OPTION, 
               JOptionPane.INFORMATION_MESSAGE,  
               null,BedRoom1Items, BedRoom1Items [0]  ); 
 break;

  case JOptionPane.NO_OPTION: 
  System.out.println ("Do you want to go to the Bathroom or Explore the 
  room?");
  int bedRoom2Explore = JOptionPane.showOptionDialog(null, 
               ("Pick an Item"), 
               "Haunted House", 
               JOptionPane.YES_NO_OPTION, 
               JOptionPane.INFORMATION_MESSAGE,  
               null,BedRoom2Items, BedRoom2Items [0]  ); 
 break;
 default:
 System.out.println("You are now in the Master Bedroom");
 int masterBedExplore = JOptionPane.showOptionDialog(null, 
               ("Do you want to go to the Bathroom or Explore the room?"), 
               "Haunted House", 
               JOptionPane.YES_NO_OPTION, 
               JOptionPane.INFORMATION_MESSAGE,  
               null,MasterBedRoomItems, MasterBedRoomItems [0]  ); }

问题在于,在第一组IF和ElSE陈述之后,它始终会键入:

“您在1楼的浴室中”,其余选项变得非常混乱且混乱。

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:2)

使用完整的State设计模式会更好。但是这里有一个示例,它将大大简化OP的当前方法,并使调试更加容易。它将消除较深的if / else(或切换)层次结构,并且可以轻松地进行调整/扩展。它还减少了很多本质上重复的代码。

基本上,请保留所有可能位置的集合,然后为播放器当前所在的位置保留一个状态变量。根据当前位置向特定房间调度。每个房间设置可能的移动。

此代码不完整(没有所有房间),枚举需要完成,必须添加有效的.toString()等,但是它显示了简单的OP代码的一般模式。此外,getAction可以显示其他文本,可以调整标题以反映当前房间(需要一个参数),等等。

enum Locations
{
    FrontDoor, LivingRoom, DiningRoom, Stairs, Bedroom1, Bedroom2, MasterBedroom, Kitchen, Pantry, Bathroom, ExploreLivingRoomItems,

    // add a .toString() for each place
}



private static String getPlayer()
{
    JFormattedTextField nameField = new JFormattedTextField();

    /* New JPanel */
    JPanel myPanel = new JPanel();
    myPanel.add(new JLabel("Enter your name to play: "));
    myPanel.add(nameField);
    myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

    int result = JOptionPane.showConfirmDialog(null,
                                               myPanel,
                                               "Enter Name",
                                               JOptionPane.OK_CANCEL_OPTION);


    if (result == JOptionPane.OK_OPTION) {
        return nameField.getText();
    }
    else {
        return "";
    }
}



private static void showMap()
{
    // show the map
}



private static Locations getAction(String playerName, Locations[] locations)
{
    Object[] opts = new Object[locations.length];
    for (int i = 0; i < opts.length; ++i) {
        opts[i] = locations[i].toString();
    }

    int res = JOptionPane.showOptionDialog(null,
                                           "Select your next move, "
                                                   + playerName,
                                           "Haunted House",
                                           JOptionPane.YES_NO_OPTION,
                                           JOptionPane.INFORMATION_MESSAGE,
                                           null,
                                           opts,
                                           opts[0]);

    return locations[res];
}



private static Locations getFrontDoor(String playerName)
{
    Locations[] locs = { Locations.LivingRoom, Locations.DiningRoom,
            Locations.Stairs };

    return getAction(playerName,
                     locs);
}



private static Locations getLivingRoom(String playerName)
{
    Locations[] locs = { Locations.Bathroom, Locations.ExploreLivingRoomItems };
    return getAction(playerName,
                     locs);
}



public static void main(String[] args)
{
    Locations loc = Locations.FrontDoor;


    final String playerName = getPlayer();
    showMap();


    boolean keepGoing = true;

    while (keepGoing) {

        switch (loc) {
        case FrontDoor:
            loc = getFrontDoor(playerName);
            break;

        case LivingRoom:
            loc = getLivingRoom(playerName);
            break;

        // all other rooms
        } // switch
    }


}

Enter Name Front Door Options Living Room Options

答案 1 :(得分:0)

如果不是,则代替它使用...

  switch(c) {   
case value1:        ...         break; 
case value2:        ...         break;  
case valueN:        ...     
default: }

代码更易于阅读..所以您了解问题出在哪里...