2014-12-11 23 views
0

我已經寫了這個代碼,現在我練,我想它把它寫在不同的或更有效的方式。基本上這個代碼要求用戶輸入一個單詞,第二個玩家用6次嘗試猜測單詞的字母,最後有最後一次機會猜測整個單詞。關於如何以簡單的方式編寫此代碼的任何建議?試圖找到一種不同的方式來寫這個代碼

static int NUM_OF_TRIES = 6; 

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Player 1 please enter the word"); 
    String word = keyboard.next(); 

    for (int i = 0; i < NUM_OF_TRIES; i++) { 
     System.out.println("Enter your guess please"); 
     String guess = keyboard.next(); 
     boolean a = true; 
     for (int j = 0; j < word.length(); j++) { 
      if (guess.charAt(0) == word.charAt(j)) { 
       System.out.println(" at position " + (j + 1)); 
       a = false; 
       break; 
      } 

     } 
     if (a) { 
      System.out.println("Sorry not letter " + guess.charAt(0)); 
      continue; 
     } 
    } 

    System.out.println("Enter your last guess: "); 
    String wordComp; 
    wordComp = keyboard.next(); 

    if (wordComp.equals(word)) { 
     System.out.println("You got it!"); 
    } else { 
     System.out.println("Sorry you lost!"); 
    } 

} 

}

+2

這段代碼的工作?如果是,那麼你可以在[代碼審查]張貼(http://codereview.stackexchange.com/) – Baby 2014-12-11 01:03:13

+1

如果你只是希望它是更易於閱讀,你可以提取出內部的字符比較函數的循環,爲了可讀性。 – echen 2014-12-11 01:04:09

+1

的繼續不需要......代碼只會告訴你的信件大約一個這麼說的話球你會得到3不3和4 – 2014-12-11 01:21:47

回答

0

---所有你必須首先要確保

word.length <=guess.length 

或者你會碰到一個例外.---編輯:這是不是OBV正確

現在不能BC我測試我的手機,但據我所看到的,你會碰到的問題,如果猜詞具有相同的信多次,因爲你打破在找到相同的第一個字母后退出循環。

正如在評論中提到,比較可以通過一種方法做這樣

private static List<Integer> getLetterIndices(String word, char letter); 

然後,你將不再需要你的布爾指明正確的猜測,但指數的列表中找到

和當然,你可以做一個面向對象的方法而不是靜態主要方法(不是說它的實現更快或更好的性能,只是爲了練習),或許是這樣的:

public class WordToGuess{ 
    private Map<Character,List<Integer>> letter2indices;//... 
    public WordToGuess(String word){ 
     parseIndices(word); 
    } 
    //parse indices of each letter to the map 
    private void parseIndices(String word); 

    public List<Integer> getLetterIndices(char letter); 
} 
+0

感謝您的幫助我欣賞它! – rojo 2014-12-11 02:30:55

0

嗯,這裏是一個較短的版本:

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Player 1 please enter the word"); 
    String word = keyboard.next(); 

    for (int i = 0; i < NUM_OF_TRIES; i++) { 
     System.out.println("Enter your guess please"); 
     String guess = keyboard.next(); 
     int index = word.indexOf(guess.charAt(0)); 

     if (index == -1) 
      System.out.println("Sorry not letter " + guess.charAt(0)); 
     else 
      System.out.println(" at position " + (index + 1)); 
    } 

    System.out.println("Enter your last guess: "); 
    String wordComp = keyboard.next(); 

    if (wordComp.equals(word)) 
     System.out.println("You got it!"); 
    else 
     System.out.println("Sorry you lost!"); 
} 
+0

只是想提一提,這與例如字「信」 UND猜「E」或「T」 BC它只是導致一個地方同樣的問題(「2」 /「3」),而不是所有的地方;) – 2014-12-11 03:04:52

+0

確實可以改進原有的邏輯。然而請注意,作者專門要求一種「簡單的方式」來編寫相同的程序邏輯,而不是以任何其他方式更健壯或更好的版本。 – ulix 2014-12-11 07:51:25

相關問題