2015-02-24 68 views
-3
public static int atk = 5; 

    public static void main(String[] args){ 
     System.out.println("DUNGEONS AND DWAGONS"); 
     Scanner scan = new Scanner(System.in); 
     help(); 


     String input = scan.next(); 

    // when someone types help first it works, but when they type stats after it shows what would happen if someone typed help.  
     while(true){ 
      if (input.equals("help")){ 
       help(); 
       scan.next(); 

      }else if(input.equals("stats")){ 
       stats(); 
       scan.next(); 
      } 
     } 
    } 
    static void help() { 
     System.out.println("type n,s,e,w to move in a direction"); 
     System.out.println("type stats to see your stats"); 
     System.out.println("type look and then a direction n,e,s,w see the sights"); 
     System.out.println("if you wish to see this again type help"); 

    } 

    static void stats(){ 
     System.out.println("Health " + health); 
     System.out.println("Armour " + armour); 
     System.out.println("Attack " + atk); 

    } 

} 

//我已經嘗試每一件事情,如果你鍵入任何內容後的第一個THIG鍵入它會一遍又一遍的執行同樣的事情。當用戶輸入第二個字符串,它執行的第一個類型

+4

'輸入'是否改變過? – 2015-02-24 15:49:15

+1

@SotiriosDelimanolis帶領你朝着正確的方向前進。在第一次之後,您不會將掃描儀讀數分配給「輸入」變量。 – Kon 2015-02-24 15:50:21

+0

我該如何解決,然後 – Jacob 2015-02-25 15:04:21

回答

5

只是移動scan.next的if/else語句外,並將其分配給輸入變量,如:

if (..) { 
    help(); 
} else { 
    stats(); 
} 
input = scan.next(); 

,並希望退出,所以你可以來while循環並正常終止程序。

+0

感謝您的幫助 – Jacob 2015-02-25 15:19:54

+0

@Jacob你可以接受答案,如果它有幫助,它將有助於未來將面臨同樣問題的其他用戶。 – SMA 2015-02-26 07:07:29

相關問題