赢取检测Hangman游戏不起作用?

时间:2016-06-02 19:47:25

标签: java if-statement

我差不多完成了一个模拟Hangman游戏的程序,但我无法弄清楚为什么我的Win检测方法(有一个计数器变量corLetters,每次打印出正确的字母时都会增加,然后包括if-statement

if(corLetters == word.length())

其后是通知用户获胜的操作无效。

供参考,出现这种情况:

再一次跟着“你猜到了什么字母?”等等,每次猜测都注册为不正确。这是我的课程:

我的主要课程:

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();

        }

    }
} 

此外,GetData类:

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(); }

}

谢谢:)

1 个答案:

答案 0 :(得分:1)

有两个错误。

首先,你应该将你的胜利检测放在主循环中:

0

显示字功能还存在另一个问题。您可能忘记在函数开始时将corLetters重置为0。

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

}
while(incGuessCount<5 && gameOver == false);

通过这些更改,它应该可以正常工作。但是,下次,为了避免这种错误,我建议你使用更清晰的功能名称。例如,&#34; displayWord&#34;这里还计算正确字母的数量。这是一种不好的做法,因为你有时会忘记这个事实并犯错误:))

正如评论中所建议的,你甚至应该将你的功能分成两个子功能,因为displayWord确实做了两件不同的事情。