2011-04-20 107 views
-2

我正在做的Zork克隆,每當它循環,一個錯誤在Eclipse中出現:爲什麼我的Java Zork Clone不能正確地循環?

import java.util.Scanner; 
public class Level1 { 
public static void main(String[] args) { 
    int x; 
    for(x=1; x<10; x++) { 
     System.out.println ("Welcome to Sork - by Wyatt Lucas"); 
     System.out.println (" "); 
     System.out.println ("Do you want to play?"); 
     Scanner first = new Scanner(System.in); 
     @SuppressWarnings("unused") 
     String firstInput; 
     firstInput = first.nextLine(); 
     System.out.println("Well, it doesn't matter!"); 
     System.out.println("Use commands such as LOOK and GO NORTH \nto complete your adventure.");  
     System.out.println(""); 
     System.out.println("You are in a room."); 
     Scanner second = new Scanner(System.in); 
     String secondInput = second.nextLine(); 
     String look = "look"; 
     if(secondInput.equalsIgnoreCase(look)) { 
      System.out.println("You look around and see a DOOR \nand a KEY on the floor."); 
     } 
     else { 
      //don't use System.err.println. Just use System.out.println 
      System.out.println("I do not understand that."); 
      continue; 
     } 
     Scanner third = new Scanner(System.in); 
     String thirdInput = third.nextLine(); 
     String pick_up_key = "pick up key"; 
     if(thirdInput.equalsIgnoreCase(pick_up_key)) { 
      System.out.println("You picked up the KEY."); 
     } else { 
      System.out.println("I do not understand that."); 
      continue; 
     } 
     Scanner fourth = new Scanner(System.in); 
     String fourthInput = fourth.nextLine(); 
     String open_door = "open door"; 
     if(fourthInput.equalsIgnoreCase(open_door)) { 
      System.out.println ("You open the door and are immediately \neaten by a grue!"); 
     } else { 
      System.out.println("I do not understand that."); 
     } 

     first.close(); 
     second.close(); 
     third.close(); 
     fourth.close(); 
     try { 
      Thread.sleep(1997); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 
} 
+0

您還沒有報告問題所在。 – bmargulies 2011-04-20 02:16:05

+0

什麼是錯誤? – 2011-04-20 02:17:51

+0

爲什麼要構建一個新的掃描儀來閱讀每一行?您可以重新使用相同的掃描儀。 – davmac 2011-04-20 02:23:28

回答

0

運行代碼來開門的點給出

Exception in thread "main" java.util.NoSuchElementException: No line found 
at java.util.Scanner.nextLine(Scanner.java:1516) 
at Level1.main(Level1.java:12) 

這是發生因爲你在最後用你所有的third.close()調用關閉System.in。只做一個掃描儀,不要關閉它。

相關問題