Java Hangman游戏的Win检测不起作用?

Java Hangman游戏的Win检测不起作用?,java,if-statement,Java,If Statement,我几乎完成了一个模拟刽子手游戏的程序,但我不明白为什么我的胜利检测方法(有一个计数器变量corlets,它在每次打印正确的字母时递增,然后包括if语句: if(corLetters == word.length()) 然后是通知用户win不起作用的操作 作为参考,出现以下情况: 接着是“你猜的是什么字母?”等等,所有的猜测都是错误的。下面是我的课程: 我的主要班级: import java.util.Scanner; import java.util.Random; public clas

我几乎完成了一个模拟刽子手游戏的程序,但我不明白为什么我的胜利检测方法(有一个计数器变量
corlets
,它在每次打印正确的字母时递增,然后包括
if语句

if(corLetters == word.length())
然后是通知用户win不起作用的操作

作为参考,出现以下情况:

接着是“你猜的是什么字母?”等等,所有的猜测都是错误的。下面是我的课程:

我的主要班级:

import java.util.Scanner;
import java.util.Random;

public class Main 
{

    public static void main(String[] args) 
    {
        Hangman manHang = new 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
    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();

        }

    }
} 

谢谢:)

有两个错误

首先,您应该将win detection置于主循环中:

do
{
    drawGallows();
    displayWord();
    getGuess();
    checkGuess();
    // ---> You should check win here
    if(corLetters == word.length())
        gameOver = true;

}
while(incGuessCount<5 && gameOver == false);
do
{
绞刑架();
displayWord();
getGuess();
checkGuess();
//-->您应该在此处选中win
if(corLetters==word.length())
gameOver=true;
}

while(incguesscount这应该在
do while
循环中:
if(corlets==word.length())gameOver=true;
?旁白:您应该清晰地格式化代码。此外,不要在
if
else
for
子句周围使用大括号,这很容易导致意外逻辑。另外,添加打印语句也可能有助于调试。您可以尝试添加
System.out.println(回答:“+word.length());
System.out.println(“正确的字母:+corlets”)在HangMan类中执行do-while循环的过程中。我发现您的正确字母没有正确递增。组织代码肯定是我的一大难题,但我肯定会在我的下一个项目中遵循所有这些建议。谢谢大家,我很感激。我甚至会说您的函数是否在做两个事物(检查有效性和显示)你应该把它分成两个函数。另外,使用公共状态也有一个问题:尝试使你的函数无状态。我肯定在这个项目中使用了快捷方式,这完全是我的错。非常感谢你们的投入,我一定会在下一个项目中牢记这一建议。
do
{
    drawGallows();
    displayWord();
    getGuess();
    checkGuess();
    // ---> You should check win here
    if(corLetters == word.length())
        gameOver = true;

}
while(incGuessCount<5 && gameOver == false);
public boolean displayWord()            
{
    corLetters = 0;  // <---- You should add this line here
    boolean goodGuess = false;automatically
    char letter = guessedLetter.charAt(0);

    for(int i = 0;i<word.length();i++)
        if (lettersFound[i]==true)
        {
         System.out.print(word.charAt(i)+" ");
         corLetters++;
        }
        else if (word.charAt(i)==letter)
        {
            System.out.print(word.charAt(i)+" ");
            lettersFound[i] = true;
            goodGuess = true; 
            corLetters++; 
        }
        else
            System.out.print("_ ");

    return goodGuess;           
}