我是编程和java的新手(正如你所看到的),我真的陷入编程hangman的基本问题。我的代码并不是我所知道的最干净的代码,但我真的想对逻辑明智的错误提出一些建议。我的麻烦是让程序输出' hangman display'每次猜测都正确。我认为我尝试错误++设置的方式存在问题。我也无法让重复的信件正常工作,但却无法弄明白。
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;
}
}
答案 0 :(得分:1)
if (!letterFound)
{
letterFound = false;
如果!letterFound
为真,则表示letterFound
已为假。因此,之后无需将其设置为false。这是一个逻辑缺陷。
我建议将letterFound = false;
移到while循环的开头。所以你开始每一个循环。