2013-05-10 67 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?");} 

這將導致更多的問題,它甚至沒有任何意義,真的。