gpt4 book ai didi

Java Hangman 无法正确循环

转载 作者:行者123 更新时间:2023-11-30 07:15:08 25 4
gpt4 key购买 nike

我对编程和 java 非常陌生(如你所见),并且确实陷入了编程刽子手的一个基本问题上。我的代码不是我所知道的最干净的,但我真的很想就我在逻辑上出错的地方提供一些意见。我的麻烦是让程序在每次猜测时正确输出“刽子手显示”。我认为我的 attemptsWrong++ 设置方式有问题。我也无法让重复的字母正常工作,但只是不太明白。

import java.util.*;

public class HangMan
{

public static void main(String[] args)
{
char[] alphabet =
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
String[] words =
{
"javascript", "declaration", "object", "class", "failing"
};
int randomIndex = (int) (Math.random() * words.length);
String randomWord = words[randomIndex];
char[] explodedWord = explode(randomWord);
char[] censoredWord = new char[randomWord.length()];
int triesWrong = 0;
Scanner input;
char letterGuess;
boolean letterFound = false;

for (int i = 0; i < randomWord.length(); i++)
{
censoredWord[i] = '_';
}

while (!gameWon(censoredWord, explodedWord))
{
hangMan(triesWrong, censoredWord, alphabet);
System.out.println("Please guess a letter: ");
input = new Scanner(System.in);
letterGuess = input.next().trim().charAt(0);

char[] repeatedLetters = new char[26];

for (int i = 0; i < alphabet.length; i++)
{
if (letterGuess == alphabet[i])
{
if(letterGuess == repeatedLetters[i])
{
System.out.println("You already guessed this letter, please enter another letter.");
}
else
{
repeatedLetters[i] = alphabet[i];
alphabet[i] = '_';
}
}
}
for (int i = 0; i < explodedWord.length; i++)
{
if (letterGuess == explodedWord[i])
{
censoredWord[i] = letterGuess;
letterFound = true;
}
}
if (!letterFound)
{
letterFound = false;
triesWrong++;
}
}
}

public static char[] explode(String bank) //getWord method for string bank
{
//create character array called explode that has length of string
char[] explodedWord = new char[bank.length()];

for (int i = 0; i < bank.length(); i++)
{
explodedWord[i] = bank.charAt(i);
}
return explodedWord;
}

public static void hangMan(int n, char[] dashWord, char[] alph)
{
if (n == 0)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 1)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 2)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 3)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|\t|\\");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 4)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 5)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 6)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("| /");
System.out.println("|");
System.out.println("|");
} else if (n == 7)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("| / \\");
System.out.println("|");
System.out.println("|");
System.out.println("GAME OVER");
System.exit(0);
}

System.out.println("\n\n");

for (int i = 0; i < alph.length; i++)
{
System.out.print(alph[i] + " ");
}

System.out.println("\n\n");

for (int i = 0; i < dashWord.length; i++)
{
System.out.print(dashWord[i] + " ");
}

System.out.println("\n\n");
}

public static boolean gameWon(char[] dashWord, char[] bigWord)
{
if(dashWord.length != bigWord.length)
{
return false;
}

for(int i = 0; i < dashWord.length; i++)
{
if(dashWord[i] != bigWord[i])
{
return false;
}
}
System.out.println("Congratulations, you win!");

return true;
}

}

最佳答案

    if (!letterFound)
{
letterFound = false;

如果 !letterFound 为 true,则意味着 letterFound 已经为 false。所以之后不需要将其设置为 false。这是一个逻辑缺陷。我建议将 letterFound = false; 移至 while 循环的开头。所以你每个循环都重新开始。

关于Java Hangman 无法正确循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540570/

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