2013-05-10 60 views
-7

所以这里是我的代码。否则如果错误

public static void play(Player player) { 
    Scanner localScanner = new Scanner(System.in); 
    while(localScanner.hasNextLine()){ 
     String input = localScanner.nextLine(); 
     if(input.equals("quit")) { 
     System.out.println("Game over, Good bye."); 
     return; 
     } 
     if(input.contains("north")){ 
     buildWorld(player, player.moveTo().connectNorth()); 
     } else if(input.contains("south")){ 
     buildWorld(player, player.moveTo().connectSouth()); 
     } else if(input.contains("east")){ 
     buildWorld(player, player.moveTo().connectEast()); 
     } else if(input.contains("west")){ 
     buildWorld(player, player.moveTo().connectWest()); 
     } else { 
     String contents; 
     if (!contents.equals("")) 
      System.out.println("There is:\n" + contents); 
     else{ 
      System.out.println("This room is empty."); 
     } else if(input.startsWith("pickup")){ // <-- This is the 'orphan' else 
     contents = input.substring(8); 
     if(player.moveTo().moveTo(contents)){ 
      Object localObject = player.connectWest(contents); 
      if(localObject !=null) 
      System.out.println("You have picked up " +localObject); 
      else 
      System.out.println("You have too much damage, Game over.");} 
     else { 
      System.out.println("There is no " +contents); 
     }} else if (input.startsWith("drop")){ 
     contents=input.substring(6); 
     if(player.moveTo(contents)) 
      System.out.println("you dropped " +contents); 
     else 
      System.out.println("You don't have "+ contents); 
     } else if(input.contains("status")){ 
     System.out.println(player);} 
     else{ 
     System.out.println("What?");} 
    } 
    } 
    } 

我不断收到一个没有,如果错误,我不知道如何解决它。粗体代码是发生错误的地方。我一直在研究这个问题好几个小时,但我仍然无法完成这项工作。请,请帮助我。

+0

使用标准的代码格式会有所帮助。你的缩进和支架放置是非常随机的,我们不能确定没有阅读所有的东西... – 2013-05-10 17:17:55

+2

你只是**不能**做一个'else' -'sese if' – BackSlash 2013-05-10 17:18:27

+1

其他的标准基本上是通用的在if-else链中。你不能有其他的 - 如果在它之后。 – Perception 2013-05-10 17:18:43

回答

2

这部分的问题是:

if (!contents.equals("")) 
     System.out.println("There is:\n" + contents); 
    else{ 
     System.out.println("This room is empty.");} 
    else if(input.startsWith("pickup")) { 
    // ... 
    } 

else后,您不能else if

5

我认为问题是你有一个else if跟在else之后。尝试将else移至此序列的最后

此外,你必须遵循同样的if 2点else声明:

else { 
    String contents; 
    ... 
} 
... 
else{ 
    System.out.println("What?");} 

这将导致更多的问题,它甚至没有任何意义,真的。