【发布时间】:2015-10-29 22:43:13
【问题描述】:
所以这有点模糊,但我有一个程序应该是一个基于 texed 的冒险游戏。到目前为止,我的代码工作正常,但我无法让我的命令从一个房间移动到另一个房间工作。 这是我的三个相关课程:
但我想把重点放在这部分:
// takes the user's input and puts it into a string array
String[] parts = input.split("\\s+");
//if the user inputs equals go
else if (parts.length == 2 && parts[0].equalsIgnoreCase("go"))
{
//if the second part of the input is nth
if(parts[1].equalsIgnoreCase("nth"))
{
//gets the room the player is in
Room temp = getRoomLoc(player.getLocation());
//if the player can move north
if(temp.canMoveNth())
{
//sets the player's location to the room to the north
player.setLocation(temp.getNth());
}
//if the player can't move north this checks why
if(!temp.canMoveNth())
{
//if there is a door to the north
if(temp.getNth()>-1)
{
//checks if the door is locked
if(temp.getRoomlock().NthLocked)
{
//this code shouldn't run if there isn't a door
System.out.println("The Door to the north is "
+ "locked");
}
else
{
//if this runs something is FUBAR
System.out.println("Error, you broke it: North");
}
}
//if there isn't a door to the north
else
{
System.out.print("There is no door to the north.");
}
}
}
//if the second part of the player's input is south
else if(parts[1].equalsIgnoreCase("sth"))
{
//gets the player's location
Room temp = getRoomLoc(player.getLocation());
//if the player can move south
if(temp.canMoveSth())
{
//sets the players location to the south
player.setLocation(temp.getSth());
}
//if the player can't move south
if(!temp.canMoveSth())
{
//if there is a door
if(temp.getSth()>-1)
{
//checks if it is locked
if(temp.getRoomlock().SthLocked)
{
//this code shouldn't run if there isn't a door
System.out.println("The Door to the south is "
+ "locked");
}
else
{
//if this runs something is FUBAR
System.out.println("Error, you broke it: South");
}
}
//if there isn't a door
else
{
System.out.print("There is no door to the south.");
}
}
}
//if the second part of the player's input is east
else if(parts[1].equalsIgnoreCase("est"))
{
//gets the the player's location
Room temp = getRoomLoc(player.getLocation());
//if the player can move east
if(temp.canMoveEst())
{
//sets the player's location to the east
player.setLocation(temp.getEst());
}
//if the player can't move east
if(!temp.canMoveEst())
{
//if there is a door
if(temp.getEst()>-1)
{
//checks if it is locked
if(temp.getRoomlock().EstLocked)
{
//this code shouldn't run if there isn't a door
System.out.println("The Door to the east is "
+ "locked");
}
else
{
//if this runs something is FUBAR
System.out.println("Error, you broke it: East");
}
}
//if there is no door
else
{
System.out.print("There is no door to the east.");
}
}
}
//if the second part of the player's input is west
else if(parts[1].equalsIgnoreCase("wst"))
{
//gets the player's location
Room temp = getRoomLoc(player.getLocation());
//if the player can move west
if(temp.canMoveWst())
{
//sets the player's location to the west
player.setLocation(temp.getSth());
}
//if the player can't move west
if(!temp.canMoveWst())
{
//if there is a door
if(temp.getWst()>-1)
{
//checks if it is locked
if(temp.getRoomlock().SthLocked)
{
//this code shouldn't run if there isn't a door
System.out.println("The Door to the west is "
+ "locked");
}
else
{
//if this runs something is FUBAR
System.out.println("Error, you broke it: West");
}
}
//if there is no door
else
{
System.out.print("There is no door to the west.");
}
}
}
else
{
System.out.println("\""+ parts[1] +"\""+" is not a valid "
+ "direction");
}
我希望它检测输入的第一部分何时是“go”,第二部分是“nth/sth/est/wst”并且我的其他命令有效,但是使用此命令它要么检测不到“go”或者当我调用“parts[1]”但它的长度为 2 时,它会给我的数组一个超出范围的异常?
【问题讨论】:
-
听说过
if、elseif和else条件语句? -
是的,这只是一个片段。我放了一个完整代码的链接。
-
那么是什么阻止你使用这些来实现你想要的?
-
您得到的确切错误是什么?
-
当我在空格处拆分刺“输入”时的数组列表“部分”,即使长度是两个(假设您输入了两个单词),如果我调用“部分 [1]”它给出索引越界异常。
标签: java arrays string input string-split