我的代码有三个主要问题是复制一个Hangman游戏:
public class Main
{
public static void main(String[] args)
{
Hangman manHang = new Hangman();
}
}
The Hangman class:
import java.util.Random;
public class Hangman
{
Random r;
GetData get;
String[] Bank = {"consider","minute","accord","evident","practice","intend","concern","commit","issue","approach","establish","utter","conduct","engage","obtain","scarce","policy","straight","stock","apparent","property","fancy","concept","court","appoint","ambiguous","arbitrary","alliteration","arrogant","benevolent","belligerent","boycott","cynical","connotation","cessation","contemporary","craving","grandiose","gratuitous","guile","harbinger","impetuous","incandescent","indigent","inexorable","injunction","insipid","insurgent","languish","magnate","abjure","abrogate","abstemious", "acumen", "antebellum","auspicious","belie","bellicose","bowdlerize","chicanery","chromosome","churlish","circumlocution","circumnavigate","deciduous","deleterious","diffident","enervate","enfranchise","epiphany","equinox","evanescent","expurgate","facetious", "fallacious"};
String word;//Stores the random word used
boolean finished = false;
int incGuessCount = 0;
boolean[] lettersFound;//Used to mark which letters have been found
String guessedLetter=" ";//Used to store guesses
boolean playerHasWon = false;
public Hangman()
{
r = new Random();
get = new GetData();
word=Bank[r.nextInt(Bank.length)]; //Selects a random word and assigns word the value
lettersFound = new boolean[word.length()]; //Creates a boolean array the length of the word
int incGuessCount = 0;
while(incGuessCount<5 && playerHasWon == false)
{
drawGallows(); //Show the Gallows depending on how many incorrect guesses there are
displayWord();
getGuess();
checkGuess();
//checkVictory();
}
if (incGuessCount>=5)
{
System.out.print('\u000C');//Clears the screen
fiveWrong();//Displays full Hangman
System.out.println("You have lost! The word was "+word);
}
}
public void getGuess()
{
System.out.println("\u000C");
System.out.println(" ");
System.out.println("What letter do you guess?");
System.out.println("You have "+(5-incGuessCount)+" guesses left.");
System.out.print("Enter guess:");
guessedLetter = get.aWord();//Uses scanners to take in the guesses
}
public boolean displayWord()
{
boolean goodGuess = false;//Assumes guess is bad automatically
char letter = guessedLetter.charAt(0);
for(int i = 0;i<word.length();i++)//Goes through all the letters to check guess's status
if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
{
System.out.print(word.charAt(i)+" ");
}
else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
{
System.out.print(word.charAt(i)+" ");
lettersFound[i] = true;
goodGuess = true;
}
else//Fills in non-applicable spaces with an underscore
System.out.print("_ ");
return goodGuess;
}
public void checkGuess()
{
if(!displayWord() && incGuessCount==5)
fiveWrong();
else if(!displayWord() && incGuessCount<5)
{
incGuessCount++;
drawGallows();
getGuess();
displayWord();
}
else
{
drawGallows();
getGuess();
displayWord();
}
}
public void defaultMan()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void oneWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void twoWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void threeWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | /| ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void fourWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | /|\\ ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void fiveWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | ( ) ");
System.out.println(" | /|\\ ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
System.out.println("You have lost! The word was "+word+".");
System.out.println("Rerun the program to try again.");
finished=true;
}
public void drawGallows()
{
if(incGuessCount==0)
{
defaultMan();
}
if(incGuessCount==1)
{
oneWrong();
}
if(incGuessCount==2)
{
twoWrong();
}
if(incGuessCount==3)
{
threeWrong();
}
if(incGuessCount==4)
{
fourWrong();
}
if(incGuessCount==5)
{
fiveWrong();
}
}
}
And the GetData class:
import java.util.Scanner;
public class GetData
{
private Scanner input;
public GetData()//Produces a scanner to take in input
{ input = new Scanner(System.in); }
public String aWord()//Gets the input as a guess/string
{ return input.next(); }
public int aNumber()//Gets the input as a number
{ return input.nextInt(); }
}
抱歉组织不好,我是编码新手.我已经在多个人的帮助下挖掘了我的代码几个小时,但我们无法弄清楚我的逻辑失败的地方,而且显然至少在几个方面都有.任何帮助都会受到赞赏,即使它只是指出了更微小的有缺陷的逻辑或无用的变量.谢谢.
更新:非常感谢那些贡献者,这些答案帮助我修复了程序中的所有错误.我现在在我的胜利检测中还有一个问题.我已经创建了一个变量gameOver,当用户超出他的错误猜测次数时,它会存储true,并且每当打印一个字母而不是下划线时我也会增加它.然后我创建了两个if语句:
if(corLetters == word.length())
gameOver = true;
if (incGuessCount<5 && gameOver)
{
System.out.println("\u000c");
System.out.println("Congratulations!");
System.out.println("You have won!");
System.out.println("Rerun the program to try again.");
}
但是游戏没有注册胜利.再次,非常感谢您的帮助,如果您有任何建议来修复或改进我的胜利检测,那将非常感谢:)
在我的问题解决后,这是我的新Hangman类:
import java.util.Random;
public class Hangman
{
Random r;
GetData get;
String[] Bank = {"consider","minute","accord","evident","practice","intend","concern","commit","issue","approach","establish","utter","conduct","engage","obtain","scarce","policy","straight","stock","apparent","property","fancy","concept","court","appoint","ambiguous","arbitrary","alliteration","arrogant","benevolent","belligerent","boycott","cynical","connotation","cessation","contemporary","craving","grandiose","gratuitous","guile","harbinger","impetuous","incandescent","indigent","inexorable","injunction","insipid","insurgent","languish","magnate","abjure","abrogate","abstemious", "acumen", "antebellum","auspicious","belie","bellicose","bowdlerize","chicanery","chromosome","churlish","circumlocution","circumnavigate","deciduous","deleterious","diffident","enervate","enfranchise","epiphany","equinox","evanescent","expurgate","facetious", "fallacious"};
String word;//Stores the random word used
boolean finished = false;
int incGuessCount = 0;
int corLetters = 0;
boolean[] lettersFound;//Used to mark which letters have been found
String guessedLetter=" ";//Used to store guesses
boolean gameOver = false;
public Hangman()
{
r = new Random();
get = new GetData();
word=Bank[r.nextInt(Bank.length)]; //Selects a random word and assigns word the value
lettersFound = new boolean[word.length()]; //Creates a boolean array the length of the word
do
{
drawGallows(); //Show the Gallows depending on how many incorrect guesses there are
displayWord();
getGuess();
checkGuess();
}
while(incGuessCount<5 && gameOver == false);
if (incGuessCount>=5)
{
fiveWrong();//Displays full Hangman
}
if(corLetters == word.length())
gameOver = true;
if (incGuessCount<5 && gameOver)
{
System.out.println("\u000c");
System.out.println("Congratulations!");
System.out.println("You have won!");
System.out.println("Rerun the program to try again.");
}
}
public void getGuess()
{
System.out.println("\u000C");
System.out.println(" ");
System.out.println("What letter do you guess?");
System.out.println("You have "+(5-incGuessCount)+" guesses left.");
System.out.print("Enter guess:");
guessedLetter = get.aWord();//Uses scanners to take in the guesses
}
public boolean displayWord()
{
boolean goodGuess = false;//Assumes guess is bad automatically
char letter = guessedLetter.charAt(0);
for(int i = 0;i<word.length();i++)//Goes through all the letters to check guess's status
if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
{
System.out.print(word.charAt(i)+" ");
corLetters++;
}
else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
{
System.out.print(word.charAt(i)+" ");
lettersFound[i] = true;
goodGuess = true;
corLetters++;
}
else//Fills in non-applicable spaces with an underscore
System.out.print("_ ");
return goodGuess;
}
public void checkGuess() {
boolean disW = displayWord();
if (!disW && incGuessCount == 5)
fiveWrong();
else if (!disW && incGuessCount < 5) {
incGuessCount++;
}
}
public void defaultMan()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void oneWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void twoWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void threeWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | /| ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void fourWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | /|\\ ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
}
public void fiveWrong()
{
System.out.println('\u000C');
System.out.println(" ________");
System.out.println(" | | ");
System.out.println(" | ( ) ");
System.out.println(" | /|\\ ");
System.out.println(" | / \\ ");
System.out.println(" |_________ ");
System.out.println(" ");
System.out.println("You have lost! The word was "+word+".");
System.out.println("Rerun the program to try again.");
gameOver=true;
}
public void drawGallows()
{
if(incGuessCount==0)
{
defaultMan();
}
if(incGuessCount==1)
{
oneWrong();
}
if(incGuessCount==2)
{
twoWrong();
}
if(incGuessCount==3)
{
threeWrong();
}
if(incGuessCount==4)
{
fourWrong();
}
if(incGuessCount==5)
{
fiveWrong();
}
}
}
最佳答案 “填充”部分可以打印两次的原因是因为您使用打印单词并确定字符是否正确.我的意思是当你在checkGuess()里面有这个时:
if(!displayWord() && incGuessCount==5)
fiveWrong();
else if(!displayWord() && incGuessCount<5)
你正在if语句的决策部分调用displayWord()(打印单词,如果猜测好,则返回true).这会导致在checkGuess()方法中打印单词.这可以通过制作checkWord()来解决,例如:
public boolean checkWord()
{
boolean goodGuess = false;//Assumes guess is bad automatically
char letter = guessedLetter.charAt(0);
for(int i = 0;i<word.length();i++){//Goes through all the letters to check guess's status
if (word.charAt(i)==letter)
{
lettersFound[i] = true;
goodGuess = true;
}
}
return goodGuess;
}
然后将在if语句的决策部分中使用它.
导致图表未及时更新的第二件事是你在checkGuess()中做了太多.你要求他们进行新的猜测,而真正应该由主{}进行处理.编辑后的版本可能如下所示:
public void checkGuess()
{
if(!checkWord() && incGuessCount==5)
fiveWrong();
else if(!checkWord() && incGuessCount<5)
{
incGuessCount++;
}
}
此外,在您的构造函数中,您创建另一个计数器,使得循环在玩家输掉后失败(因为在全局变量之前使用同名的局部变量),请参阅以下代码:
r = new Random();
get = new GetData();
word=Bank[r.nextInt(Bank.length)];
lettersFound = new boolean[word.length()];
int incGuessCount = 0; // <<< This shouldn't be here
唯一剩下的就是进行胜利检测,我认为你很顺利!
编辑:
对于你的胜利检测,我喜欢你决定计算用户猜对的字符数的方式.唯一的问题是,在displayWord()中,每次打印单词时都会添加正确的字母数,而不是仅在添加新的正确字符时添加.这种改变会使:
if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
{
System.out.print(word.charAt(i)+" ");
corLetters++;
}
else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
{
System.out.print(word.charAt(i)+" ");
lettersFound[i] = true;
goodGuess = true;
corLetters++;
}
进入这个:
if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
{
System.out.print(word.charAt(i)+" ");
//Deleted line here
}
else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
{
System.out.print(word.charAt(i)+" ");
lettersFound[i] = true;
goodGuess = true;
corLetters++;
}
另一个问题是,你只能检查游戏是否在主要做的时间之外用户赢了.这可以通过将if语句移动到checkGuess()中来解决.如:
public void checkGuess() {
boolean disW = displayWord();
if (!disW && incGuessCount == 5)
fiveWrong();
else if (!disW && incGuessCount < 5) {
incGuessCount++;
}
if (corLetters == word.length()){
gameOver = true;
}
}