2015-09-25 59 views
1

我无法弄清楚如何比较用户猜测的单词中的字母,并将空格更改为猜测的字母。如何让单词中的字母显示为Hangman游戏的下划线?

package hangman; 

import java.util.ArrayList; 
import java.util.Random; 
import java.util.Scanner; 

public class HangMan { 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // user intro 
     System.out.println("Hello welcome to HangMan"); 
     System.out.println("The obecjt of this game is to guess all the letters of the word."); 
     System.out.println("You get three tries"); 

     //create an arraylist to store the words that will be guessed 
     ArrayList<String> wordBank = new ArrayList<>(); 

     //assign words to the wordbank in the array list 
     wordBank.add("car"); 
     wordBank.add("house"); 
     wordBank.add("girl"); 
     wordBank.add("boy"); 
     wordBank.add("farm"); 

     //create the random object generator 
     Random rand = new Random(); 

     //this variable holds the random number generated 
     int numberStored; 
     numberStored = rand.nextInt(5); 

     //the random number generated will link to the index of the arraylist to generate the random word 
     String randomWord; 
     randomWord = wordBank.get(numberStored); 

     //display dots to show number of letters in the word 
     for (int j = 0; j < randomWord.length(); j++) { 
      System.out.printf("_ "); 
     } 
     //create scanner object 
     Scanner input = new Scanner(System.in); 

     //this while loop controls the number of guesses 
     int wrongGuesses = 0; 
     while (wrongGuesses != 3) { 
      //prompt 
      System.out.printf("Guess a letter you think is in the word"); 

      //create a variable to hold the guessed letter 
      String guessLetter = input.nextLine(); 

      //check to see if guessed letter is in the random word 
      boolean value = randomWord.contains(guessLetter); 

      // if else statment to decide what to do 
      if (value == true) { 
       System.out.println("Letter " + guessLetter + " is correct"); 
       System.out.println("Now guess a different letter"); 
       //find the position of the guessed letter in the word 
       //char g = randomWord.charAt(0); 
       //loop through all the letters of the word 
       //for (int j = 0; j < randomWord.length(); j++){ 
       // if (gueesLetter.eq) 
       // int comparedstring 
       //} 
      } else { 
       System.out.println("Sorry "+guessLetter+ " is not in the word"); 
       System.out.println("Try again"); 
       wrongGuesses = wrongGuesses + 1; 
      } 
      System.out.println("random word = " + randomWord); 
     } 
    } 
} 

我需要匹配被猜测的字母并将其替换为_ _ _设置。要做到这一点

+1

请单独做的功课。你应该学习。 SO的复制品不是学习。 –

回答

0

一种方法是:

public static void main(String[] args) { 
    String currentWord = "hello"; 

    List<Character> guessedCharacters = new ArrayList<Character>(); 
    guessedCharacters.add('e'); 
    guessedCharacters.add('o'); 
    guessedCharacters.add('q');  
    guessedCharacters.add('i'); 

    System.out.println(maskString(guessedCharacters, currentWord)); 
} 

private static String maskString(List<Character> guessedCharacters, String currentWord){ 
    char[] currentWordList = currentWord.toCharArray(); 

    StringBuilder maskedString = new StringBuilder(); 
    int tempLength; 

    for(char letter : currentWordList){ 
     tempLength = maskedString.length(); //store the current length of the string 
     for(char guessedLetter : guessedCharacters){ 
      if(letter == guessedLetter){ 
       maskedString.append(letter); 
      } 
     } 
     if(tempLength == maskedString.length()){// if the string is still the same length, 
             // then the tested letter hasn't been guessed yet. 
      // if the letter hasn't been guessed the we 'mask' it with an underscore. 
      maskedString.append('_'); 
     } 
    } 

    return maskedString.toString(); 
} 

输出:

_e__o

+0

我不明白 – shaun

+0

这有点倒退。当然你不想显示玩家没有猜到的单词中的所有字母 – CubeJockey

+0

@shaun我已经更新了我的答案以提供解决方案 –