我正在创建一个游戏,用户必须猜出单词中的字母,如果他们猜错了,如果他们猜错了就会显示单词中的字母,它会减去生命。问题是,如果他们猜对了,就打印出英菲尼迪这个词。 这是我的代码:
char words[] = new char[getRandomPirateWord().length()];
char words2[] = new char[getRandomPirateWord().length()];
for(int i = 0; i < getRandomPirateWord().length(); i++)
{
words[i] = getRandomPirateWord().charAt(i);
words2[i] = (getRandomPirateWord().charAt(i)==' ')?' ':'*';
}
while(getGuessLeft()>0)
{
getLettersGuesed().add(letter);
if (!randomPirateWord.contains(letter+""))
{
setGuessLeft(getGuessLeft() - 1);
System.out.println("You lost a life, now have " + getGuessLeft());
}
for(int i1 = 0; i1 <getRandomPirateWord().length(); i1++)
{
if(words[i1] == letter)
{
words2[i1] = letter;
}
}
for(int i1 = 0; i1 < getRandomPirateWord().length(); i1++)
{
System.out.print(words2[i1]);
答案 0 :(得分:2)
问题似乎是while循环永远不会终止。这取决于如果您猜测单词内的字母,则while循环开始,因为您永远不会更改变量字母的值
while(getGuessLeft()>0) {
getLettersGuesed().add(letter);
if (!randomPirateWord.contains(letter+"")) {
setGuessLeft(getGuessLeft() - 1);
System.out.println("You lost a life, now have " + getGuessLeft());
}
for(int i1 = 0; i1 <getRandomPirateWord().length(); i1++) {
if(words[i1] == letter) words2[i1] = letter;
}
for(int i1 = 0; i1 < getRandomPirateWord().length(); i1++){
System.out.print(words2[i1]);
}
}