2016-03-10 53 views
-1

我想寫一個Hang子手遊戲項目,其中用戶可以有15個猜測和4個空間來檢查。數字(空格)由空格分隔。這是我的代碼,但由於某些原因它不起作用。任何幫助表示讚賞!如何讀取白色空間之間的整數?

System.out.println("\nPlease enter the letter you want to guess: "); 
     char guessLetter = input.next().charAt(0); 

     if (Character.isLetter(guessLetter)){ 
      System.out.println("Please enter the spaces you want to check (separated by spaces): "); 
      String guessSpaces = input.next(); 


      for (int index = 0; guessSpaces.charAt(index) == ' ';index++){ 
       if(guessSpaces.charAt(index)== secretWord.indexOf(guessLetter)){ 
        System.out.println("You guess is in the word"); 
+0

看起來你沒有正確複製程序(沒有右花括號)。 – Michael

+0

是啊!我只複製了我有疑問的部分。我不想讓任何人認爲我需要整個代碼的幫助lol – minnie

+0

你的for循環對我來說沒有任何意義。你能解釋一下你認爲for循環正在做什麼嗎? – pczeus

回答

0

我真的不理解你的代碼,但如果你想讀一些整數,這是如何做到這一點:

String[] strings = input.nextLine().split(" "); 
ArrayList<Integer> integers = new ArrayList<>(); 
for (String s : strings) { 
    if (s.trim().equals("")) { 
     continue; 
    } 
    integers.add(Integer.parseInt(s)); 
} 

現在,你必須存儲在integers整數列表。例如,如果我輸入

90 68 6 786 

數組列表將包含這個。但是,如果您在那裏輸入了一些無效值,則會引發異常。

此外,它似乎是你的for循環檢查用戶輸入的字母是否在祕密字。無需爲此使用for循環!只要這樣做:

if (secretWord.contains(Character.toString(guessLetter))) { 
    System.out.println("Your guess is in the word!"); 
}