java - 使用 JOption 窗格的 If 和 Else 语句
问题描述
您好,我是 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 楼的浴室”,其余的选项变得非常切换和混乱。
任何建议将不胜感激。
解决方案
使用完整的State
设计模式会好得多。但这里有一个示例,它将大大简化 OP 当前的方法,并使调试更容易。它将消除深层 if/else(或 switch)层次结构,并且可以轻松适应/扩展。它还减少了大量本质上重复的代码。
基本上,保留所有可能位置的集合,然后保留玩家当前所在位置的状态变量。根据当前位置调度到特定房间。每个房间都设置了可能的动作。
此代码不完整(没有所有房间),需要完成枚举,添加有效的 .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
}
}
推荐阅读
- python - 为什么可变内置对象不能在 Python 中进行哈希处理?这有什么好处?
- charts - 如何从详细记录上的字段创建垂直折线图
- typescript - 使用 fp-ts 重写一个用记录器包装的小函数
- python - 从 unstack() 到 Pandas 中的数据框
- c++ - 整数没有正确相加,但双打工作正常
- r - 有没有办法使用 R 根据关联来挑选数据?
- c# - 未读取数据库中的值
- javascript - Firestore 错误:“函数 Query.startAfter() 需要有效的第一个参数,但未定义。”
- php - 从需要/包含 PHP 文件中调用表单会导致白屏死机
- android - BottomSheet 中的 RecyclerView 未按预期工作