2016-03-22 35 views
0

在我的文字冒险游戏的命令之一是“拿”这需要用户输入两个字母“T”那就是在房间里,他们是一个项目英寸
我已经采取了这个输入,并将其分割成命令和项目,但我遇到了与if语句的麻烦。我有第一部分检查命令部分是否等于'T',但我还必须检查这个输入是否有“item”部分。我尝试使用.isEmpty()!= null以及使用.contains()检查,如果用户输入包含项目

这里是我的代码:

public Command getCommandFromResponse(String response) throws IllegalArgumentException{ 
    String[] split = response.split(" "); 

    if (split.length < 1){ 
     throw new IllegalArgumentException("Invalid command."); 
    } 

    Command command = new Command(split[0]); 
    if (split.length >= 2) { 
     command.setItem(split[1]); 
    } 
    return command; 
} 

这是take方法:

else if(userCommand.command.equalsIgnoreCase("T") && /*if userCommand contains an item*/){     
    //Split the input String into two parts, command and item 
    userCommand = getCommandFromResponse(userInput.nextLine()); 
    if (locale.item != null) { 
     if (userCommand.item.equalsIgnoreCase(locale.item.itemName)) { 
      //add the item to the player's inventory 
      player1.inventory.add(locale.item); 
      System.out.println("\tA " + locale.item + " was added to your inventory"); 
      System.out.println("\n\tYou can view your inventory by pressing 'I' or drop an item by pressing 'D'."); 
      if (locale.item.itemName.equals("map")) { 
       System.out.println("\n\tTo view the map press 'M'."); 
      } 
      //Add the item's worth to the score and set the items worth to zero to prevent double scoring 
      player1.score += locale.item.value; 
      System.out.println("\n\t" + locale.item.value + " points have been added to your score."); 
      System.out.println("\n\tThis is your current score: "+player1.score); 
      //remove the item from the current location 
      locale.item = null; 
      break; 
     } else { 
      System.out.println("\n\tThat item is not at this location."); 
     } 
    } else { 
     System.out.println("\n\tThere is no item to pick up here"); 
    } 
}//End of Take 

这是我的命令类:

public class Command { 

    String command; 
    String item; 
    public Command(String comm){ 
     command = comm; 
    } 

    public Command(String comm, String item){ 
     this.command = comm; 
     this.item = item; 
    } 

    public void setCommand(String command){ 
     this.command = command; 
    } 

    public void setItem(String item){ 
     this.item = item; 
    } 

    public String getCommand(){ 
     return this.command; 
    } 

    public String getItem(){ 
     return this.item; 
    } 

    public String toString(){ 
     return this.command + ":" + this.item; 
    } 

} 

这是我的项目:

//Items {itemName, itemDes} 
    static Item[] items = { 
      new Item ("map","a layout of your house", 10), 
      new Item ("battery", "a double A battery", 5), 
      new Item ("flashlight", "a small silver flashlight", 10), 
      new Item ("key", "this unlocks some door in your house", 15), 
    }; 

我有更多的代码,如果这不清楚。

+0

的所有项目?一个列表?枚举? – bwfcwalshy

+0

我有一个Item类,这些Items存储在一个Item数组中。我将它们添加到我的代码:) – user5959738

+0

你得到什么错误? – BPS

回答

0

我的建议是做它的方式如下:

变化

if (userCommand.item.equalsIgnoreCase(locale.item.itemName)) { 

if (userCommand.item!=null && userCommand.item.equalsIgnoreCase(locale.item.itemName)) { 

这是最简单的方法,如果有这将不会抛出异常命令中没有项目。

(我很抱歉,如果这不是你的问题,但是这是我想你所指的问题。)你怎么有存储

+0

谢谢,唯一的问题是我必须首先检查两个字是否在第二个字后面输入,然后下一步是检查输入的第二个字是否是房间中存在的项。通过使用userCommand.item!= null,我得到了一个nullpointerexception。 – user5959738

+0

如果你在这个地方('userCommand.item')得到一个'NullPointerException',那么'userCommand'似乎有问题。检查它是否被正确初始化。 –

+0

当我将命令输入到我的游戏中时,将会出现一个空格,如果我再次点击输入,则会出现异常情况,所以我对输入错误的地方有点失落 – user5959738

相关问题