Java 我的刽子手代码不是';t立即注册错误猜测,正在打印两组下划线,等等
复制刽子手游戏的代码有三个主要问题: 将“填写”部分随机打印两次,正确的猜测将被随机记录为不正确Java 我的刽子手代码不是';t立即注册错误猜测,正在打印两组下划线,等等,java,Java,复制刽子手游戏的代码有三个主要问题: 将“填写”部分随机打印两次,正确的猜测将被随机记录为不正确 和不正确的猜测并不总是被计算在内 这是主类的代码: public class Main { public static void main(String[] args) { Hangman manHang = new Hangman(); } } The Hangman class:
和不正确的猜测并不总是被计算在内
这是主类的代码:
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(); }
}
公共类主
{
公共静态void main(字符串[]args)
{
刽子手manHang=新刽子手();
}
}
刽子手级:
导入java.util.Random;
公共级刽子手
{
随机r;
获取数据;
字符串[]银行={“考虑”、“分钟”、“协议”、“明显”、“实践”、“打算”、“关心”、“承诺”、“发布”、“接近”、“建立”、“完全”、“行为”、“参与”、“获得”、“稀缺”、“政策”、“直接”、“股票”、“明显”、“财产”、“幻想”、“概念”、“法庭”、“任命”、“含糊不清”、“武断”、“头韵”、“傲慢”、“仁慈”、“好战”、“抵制”,“愤世嫉俗的”、“内涵的”、“停止的”、“当代的”、“渴望的”、“浮夸的”、“无偿的”、“狡诈的”、“预兆的”、“冲动的”、“炽热的”、“贫穷的”、“无情的”、“禁令的”、“平淡的”、“反叛的”、“消瘦的”、“大亨的”、“放弃的”、“废除的”、“节制的”、“敏锐的”、“战前的”、“吉祥的”、“邪恶的”、“好战的”、“卑鄙的”、“狡诈的”、“染色体的”粗鲁的、迂回的、迂回的、迂回的、落叶的、有害的、羞怯的、衰弱的、特权的、顿悟的、春分的、转瞬即逝的、净化的、滑稽的、谬误的;
字符串字;//存储使用的随机字
布尔完成=假;
int incGuessCount=0;
布尔值[]lettersFound;//用于标记已找到的字母
String guessedLetter=“;//用于存储猜测
布尔playerHasWon=false;
公众刽子手()
{
r=新随机数();
get=newgetdata();
word=Bank[r.nextInt(Bank.length)];//选择一个随机字并为该字赋值
lettersFound=new boolean[word.length()];//创建一个与单词长度相同的布尔数组
int incGuessCount=0;
while(incGuessCount=5)
{
System.out.print('\u000C');//清除屏幕
FiveError();//显示完整的绞刑架
System.out.println(“你输了!单词是“+单词”);
}
}
公共无效getGuess()
{
System.out.println(“\u000C”);
System.out.println(“”);
System.out.println(“你猜是什么字母?”);
System.out.println(“您有”+(5-incGuessCount)+“猜测剩余”);
系统输出打印(“输入猜测:”);
guessedLetter=get.aWord();//使用扫描仪进行猜测
}
公共布尔显示字()
{
boolean goodGuess=false;//假设猜测自动为坏
字符字母=猜测字母。字符(0);
对于(inti=0;i,让我逐一处理这些问题
- 显示两组下划线
罪魁祸首方法:\checkGuess
罪魁祸首:
- 不正确的结果不立即打印:
问题在于#checkGuess
方法。尤其是这一部分:
else
{
drawGallows();
getGuess(); // What if I make wrong guess here? The "incGuessCount" won't increment and hence you wont see the change.
displayWord();
}
要解决此问题,只需让主循环处理所有内容。并从此方法中删除显示和检查。最终的方法体为:
public void checkGuess() {
boolean disW = displayWord();
if (!disW && incGuessCount == 5)
fiveWrong();
else if (!disW && incGuessCount < 5) {
incGuessCount++;
}
}
public void checkGuess(){
布尔disW=displayWord();
如果(!disW&&incGuessCount==5)
五个错误();
如果(!disW&&INCGUSETCOUNT<5),则为else{
incluckcount++;
}
}
只需增加此处的猜测计数,并让主循环执行所有操作,包括绘图、获取和检查,而不是在此处再次手动执行
请注意,这段代码可以进行大量优化,但我将把它留给您作为一个练习,以尝试减少条件检查的数量
编辑:感谢@StephenB提醒我注意到我的错误答案。我写这篇文章真是太傻了。之所以“填充”部分可以打印两次,是因为你用它来打印单词并确定字符是否正确。我的意思是,当你有了这篇文章时,checkGuess()
:
这将在if
语句的决策部分使用
导致图表无法及时更新的第二个原因是,您在checkGuess()中做了太多的工作。
您要求他们进行新的猜测,而这实际上应该由主在{}
中处理。编辑的版本可能如下所示:
public void checkGuess()
{
if(!checkWord() && incGuessCount==5)
fiveWrong();
else if(!checkWord() && incGuessCount<5)
{
incGuessCount++;
}
}
为此:
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++;
}
另一个问题是,您仅检查游戏是否在主do while
之外的用户赢下后结束。这可以通过将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;
}
}
public void checkGuess(){
布尔disW=displayWord();
如果(!disW&&incGuessCount==5)
五个错误();
如果(!disW&&INCGUSETCOUNT<5),则为else{
incluckcount++;
}
if(corLetters==word.length()){
gameOver=true;
}
}
由于没有立即打印错误的结果,他在从getGuess()
获取猜测后立即调用checkGuess()
。这将在drawGallows()中打印
在下一个循环迭代中。非常感谢!实际上,我对程序的最后一部分win detection遇到了问题。我更新了上面的内容,如果您有任何建议,我将不胜感激:)@NoahC我添加了如何对win detection进行一些更改,以使其能够按预期工作
public void checkGuess() {
boolean disW = displayWord();
if (!disW && incGuessCount == 5)
fiveWrong();
else if (!disW && incGuessCount < 5) {
incGuessCount++;
}
}
if(!displayWord() && incGuessCount==5)
fiveWrong();
else if(!displayWord() && incGuessCount<5)
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;
}
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
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++;
}
public void checkGuess() {
boolean disW = displayWord();
if (!disW && incGuessCount == 5)
fiveWrong();
else if (!disW && incGuessCount < 5) {
incGuessCount++;
}
if (corLetters == word.length()){
gameOver = true;
}
}