我正在创建一个刽子手程序,但我在使用单词更新方法时遇到了一些问题
I'm creating a hangman program and i'm having some issue with a word update method
当代码运行时,您要求输入字符并键入一个字母,让我们说 'e' 输出使其成为 "eeeee" 而不是在部分单词方法中填写 e。我不确定如何解决它。我认为问题出在 replaceChar 方法或 updatePartialWord 方法中。会喜欢一些帮助。谢谢!
P.S 即使有更简单的方法,我也需要保持方法输入相同!
// creates partialWord using string length then creating a new string with the same amount of dashes
public static String createPartialWord(String secretWord)
{
String newsecretWord = "";
int wordLength = secretWord.length();
while (wordLength > 0)
{
newsecretWord = newsecretWord + "-";
//System.out.print("-");
wordLength--;
}
System.out.println();
return newsecretWord;
}
//replaces character at certain ints
public static String replaceChar(String word, char c, int i)
{
//return word.replace(word.charAt(i), c);
String newText = word.replace(word.charAt(i), c);
return newText;
}
public static String updatePartialWord(String partial, String secret, char c)
{
if(c==secret.charAt(0))
{
return replaceChar(partial, c, 0);
}
if(c==secret.charAt(1))
{
return replaceChar(partial, c, 1);
}
if(c==secret.charAt(2))
{
return replaceChar(partial, c, 2);
}
if(c==secret.charAt(3))
{
return replaceChar(partial, c, 3);
}
if(c==secret.charAt(4))
{
return replaceChar(partial, c, 4);
}
return partial;
}
//Prints hangman
public static void printHangman(int guessLeft)
{
String HEAD = " ";
String BODY = " ";
String LEGS = " ";
String LEFTARM = " ";
String RIGHTARM = " ";
System.out.println("_____");
System.out.println("| |");
if (guessLeft < 6) {
HEAD = "()";
}
System.out.println("| " + HEAD);
if (guessLeft < 5) {
BODY = "||";
}
if (guessLeft < 4) {
LEFTARM = "\";
}
if (guessLeft < 3) {
RIGHTARM = "/";
}
System.out.println("| " + LEFTARM + BODY + RIGHTARM);
if (guessLeft < 2) {
LEGS = "/";
}
if (guessLeft < 1) {
LEGS += "\";
}
System.out.println("| " + LEGS);
System.out.println("|_____\n\n\n\n");
}
public static String generateSecretWord()
{
String[] names = { "alone", "apple", "anode", "abuse", "angle", "amaze","adobe","amuse",
"avoid","peter","sound","doubt","upper","lower","layer","enter","alter","boxer",
"faced", "alive","adore","names","voice","water","plate","pepsi","pizza","paste",
"hello","sugar","money","paper","while","times", "mouth","other","agile","again",
"acute","arise","argue","ankle","badge","blaze","bride","chase","sense","craze",
"dance","false","exile","drove"};
String name = names[(int) (Math.random() * names.length)];
return name;
}
public static void main(String[] args)
{
Scanner stdin = new Scanner(System.in);
String secretWord = generateSecretWord();
String partialWord = createPartialWord(secretWord);
System.out.println(" Welcome to HangMan");
System.out.println("I picked a secret word");
System.out.println();
//start for statement for main game
System.out.println("The current Partial Word is: " + partialWord);
for(int i = 6;i>0;)
{
System.out.println("The current Hangman picture is");
printHangman(i);
System.out.println("Player 2, you have " + i + " remaining guesses");
System.out.println("Would you like to guess the secret work or guess a character?");
System.out.println("Type \"word\" for word, type \"char\" for character");
String playerInput = stdin.nextLine();
String charInput = "char";
String wordInput = "word";
if(playerInput.equals(charInput))
{
System.out.println("Please type a character");
char input = stdin.nextLine().charAt(0);
if(secretWord.charAt(0)== input || secretWord.charAt(1)== input || secretWord.charAt(2)== input ||
secretWord.charAt(3)== input || secretWord.charAt(4)== input)
{
System.out.println("That character is in the secret word");
System.out.println("The current partial word is: " + updatePartialWord(partialWord, secretWord, input));
}
else
{
System.out.println("Sorry that charcater is not in the secret word");
System.out.println("The current partial word is: " + updatePartialWord(partialWord, secretWord, input));
if(i==1)
{
System.out.println("You've killed the hangman. You've lost the game");
printHangman(0);
break;
}
i--;
}
}
else
{
System.out.println("Please guess the word");
String userGuess = stdin.nextLine();
if(userGuess.equals(secretWord))
{
System.out.print("Congrats that is the secret word! You've won the game!");
System.out.println("Here is the hangman");
printHangman(i);
break;
}
else
{
System.out.println("Sorry thats not the secret word. You've lost.");
break;
}
}
}
}
}
根据 javadocs
Returns a new string resulting from replacing all occurrences of
oldChar in this string with newChar.
public static String replaceChar(String word, char c, int i) {
String newText = word.replace(word.charAt(i), c);
:
本例中的 word
是 -----
,因此您要用该字母替换位置 i
(即 -
)出现的所有字符。
因此所有 -
个字符都将成为您刚刚输入的字符。
当且仅当 secret 单词中的等效字符与刚刚输入的字符匹配时,您应该改为更改部分单词中的每个字符,例如:
public static String replaceChar(String word, String secret, char c) {
for (int i = 0; i < word.length(); i++) {
if (secret.charAt(i) == c) {
word = word.substring(0, i) + c + word.substring(i+1);
}
}
return word;
}
当然,既然你现在使用的是密字,字符的位置无关紧要,你就改变传入的内容。调用代码可以修改为:
if (c == secret.charAt(0))
return replaceChar(partial, secret, c);
if (c == secret.charAt(1))
return replaceChar(partial, secret, c);
// :
// and so on.
当然,即使用修复了,你仍然有问题:
System.out.println("The current partial word is: " + updatePartialWord(partialWord, secretWord, input));
这会输出函数计算出的新部分单词,但它实际上并没有改变部分单词。您需要以下内容:
partialWord = updatePartialWord(partialWord, secretWord, input));
System.out.println("The current partial word is: " + partialWord);
当代码运行时,您要求输入字符并键入一个字母,让我们说 'e' 输出使其成为 "eeeee" 而不是在部分单词方法中填写 e。我不确定如何解决它。我认为问题出在 replaceChar 方法或 updatePartialWord 方法中。会喜欢一些帮助。谢谢! P.S 即使有更简单的方法,我也需要保持方法输入相同!
// creates partialWord using string length then creating a new string with the same amount of dashes
public static String createPartialWord(String secretWord)
{
String newsecretWord = "";
int wordLength = secretWord.length();
while (wordLength > 0)
{
newsecretWord = newsecretWord + "-";
//System.out.print("-");
wordLength--;
}
System.out.println();
return newsecretWord;
}
//replaces character at certain ints
public static String replaceChar(String word, char c, int i)
{
//return word.replace(word.charAt(i), c);
String newText = word.replace(word.charAt(i), c);
return newText;
}
public static String updatePartialWord(String partial, String secret, char c)
{
if(c==secret.charAt(0))
{
return replaceChar(partial, c, 0);
}
if(c==secret.charAt(1))
{
return replaceChar(partial, c, 1);
}
if(c==secret.charAt(2))
{
return replaceChar(partial, c, 2);
}
if(c==secret.charAt(3))
{
return replaceChar(partial, c, 3);
}
if(c==secret.charAt(4))
{
return replaceChar(partial, c, 4);
}
return partial;
}
//Prints hangman
public static void printHangman(int guessLeft)
{
String HEAD = " ";
String BODY = " ";
String LEGS = " ";
String LEFTARM = " ";
String RIGHTARM = " ";
System.out.println("_____");
System.out.println("| |");
if (guessLeft < 6) {
HEAD = "()";
}
System.out.println("| " + HEAD);
if (guessLeft < 5) {
BODY = "||";
}
if (guessLeft < 4) {
LEFTARM = "\";
}
if (guessLeft < 3) {
RIGHTARM = "/";
}
System.out.println("| " + LEFTARM + BODY + RIGHTARM);
if (guessLeft < 2) {
LEGS = "/";
}
if (guessLeft < 1) {
LEGS += "\";
}
System.out.println("| " + LEGS);
System.out.println("|_____\n\n\n\n");
}
public static String generateSecretWord()
{
String[] names = { "alone", "apple", "anode", "abuse", "angle", "amaze","adobe","amuse",
"avoid","peter","sound","doubt","upper","lower","layer","enter","alter","boxer",
"faced", "alive","adore","names","voice","water","plate","pepsi","pizza","paste",
"hello","sugar","money","paper","while","times", "mouth","other","agile","again",
"acute","arise","argue","ankle","badge","blaze","bride","chase","sense","craze",
"dance","false","exile","drove"};
String name = names[(int) (Math.random() * names.length)];
return name;
}
public static void main(String[] args)
{
Scanner stdin = new Scanner(System.in);
String secretWord = generateSecretWord();
String partialWord = createPartialWord(secretWord);
System.out.println(" Welcome to HangMan");
System.out.println("I picked a secret word");
System.out.println();
//start for statement for main game
System.out.println("The current Partial Word is: " + partialWord);
for(int i = 6;i>0;)
{
System.out.println("The current Hangman picture is");
printHangman(i);
System.out.println("Player 2, you have " + i + " remaining guesses");
System.out.println("Would you like to guess the secret work or guess a character?");
System.out.println("Type \"word\" for word, type \"char\" for character");
String playerInput = stdin.nextLine();
String charInput = "char";
String wordInput = "word";
if(playerInput.equals(charInput))
{
System.out.println("Please type a character");
char input = stdin.nextLine().charAt(0);
if(secretWord.charAt(0)== input || secretWord.charAt(1)== input || secretWord.charAt(2)== input ||
secretWord.charAt(3)== input || secretWord.charAt(4)== input)
{
System.out.println("That character is in the secret word");
System.out.println("The current partial word is: " + updatePartialWord(partialWord, secretWord, input));
}
else
{
System.out.println("Sorry that charcater is not in the secret word");
System.out.println("The current partial word is: " + updatePartialWord(partialWord, secretWord, input));
if(i==1)
{
System.out.println("You've killed the hangman. You've lost the game");
printHangman(0);
break;
}
i--;
}
}
else
{
System.out.println("Please guess the word");
String userGuess = stdin.nextLine();
if(userGuess.equals(secretWord))
{
System.out.print("Congrats that is the secret word! You've won the game!");
System.out.println("Here is the hangman");
printHangman(i);
break;
}
else
{
System.out.println("Sorry thats not the secret word. You've lost.");
break;
}
}
}
}
}
根据 javadocs
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
public static String replaceChar(String word, char c, int i) {
String newText = word.replace(word.charAt(i), c);
:
本例中的 word
是 -----
,因此您要用该字母替换位置 i
(即 -
)出现的所有字符。
因此所有 -
个字符都将成为您刚刚输入的字符。
当且仅当 secret 单词中的等效字符与刚刚输入的字符匹配时,您应该改为更改部分单词中的每个字符,例如:
public static String replaceChar(String word, String secret, char c) {
for (int i = 0; i < word.length(); i++) {
if (secret.charAt(i) == c) {
word = word.substring(0, i) + c + word.substring(i+1);
}
}
return word;
}
当然,既然你现在使用的是密字,字符的位置无关紧要,你就改变传入的内容。调用代码可以修改为:
if (c == secret.charAt(0))
return replaceChar(partial, secret, c);
if (c == secret.charAt(1))
return replaceChar(partial, secret, c);
// :
// and so on.
当然,即使用修复了,你仍然有问题:
System.out.println("The current partial word is: " + updatePartialWord(partialWord, secretWord, input));
这会输出函数计算出的新部分单词,但它实际上并没有改变部分单词。您需要以下内容:
partialWord = updatePartialWord(partialWord, secretWord, input));
System.out.println("The current partial word is: " + partialWord);