我无法弄清楚如何比较用户猜到的单词中的字母,并将空格更改为猜到的字母。
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);
}
}
}
我需要匹配猜到的字母,并在_ _ _ setup中替换它们。
答案 0 :(得分: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