[英]How to break a String into a string array, and check if the string is one thing or another
所以这有点 vauge,但我有一个程序,它应该是一个基于 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 时,我的数组是否越界异常?
使用input.starsWith("go")
和input.endsWith("nth/sth/est/wst")
怎么样?
Java 有很多很棒的对象,比如 String,它们已经为您提供了很多有用的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.