gpt4 book ai didi

java - 在 Hangman 游戏中,如何使单词中的字母显示为下划线?

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:40 25 4
gpt4 key购买 nike

我无法弄清楚如何将用户猜测的内容与单词中的字母进行比较,并将空格更改为猜测的字母。

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);
}
}
}

我需要将猜测的字母与 _ _ _ 设置中的字母进行匹配并替换它们。

最佳答案

实现此目的的一种方法是:

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

关于java - 在 Hangman 游戏中,如何使单词中的字母显示为下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32785154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com