Java-制作子手游戏 [英] Java - Making hangman game

查看:56
本文介绍了Java-制作子手游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试制作make子手游戏时,我遇到了一个小问题.之前,我曾发表过一篇有关其他错误的文章,但现在我遇到了一个我不知道的新错误.我正在尝试验证尚未输入的字母猜测.但是它跳过了if/else语句的整个部分.当我运行此代码时:

I'm having a slight problem when trying to make a hangman game. I made a post regarding a different error before, but now I am running into a new one that I can't figure out. I'm trying to verify that the letter guess wasn't already entered. But it is skipping an entire section of the if/else statement. When I run this code:

公共类TestingStuff {

public class TestingStuff {

static StringBuffer randomWord;
static Scanner console = new Scanner(System.in);
static int totalTries = 1;
static String guess;
static char finalGuess;

public static void main(String[] args) throws Exception {
    randomWord = TestingStuff.sendGet();
    char[] guesses = new char[26];
    int length = randomWord.length();

    System.out.print("* * * * * * * * * * * * * * *"
            + "\n*    Welcome to Hangman!    *"
            + "\n* * * * * * * * * * * * * * *");
    System.out.println("\nYou get 10 tries to guess the word by entering in letters!\n");
    System.out.println(randomWord);
    /*
     Cycles through the array based on tries to find letter
     */
    while (totalTries <= 10) {
        System.out.print("Try #" + totalTries + "\nWord: " + makeDashes(randomWord));

        //Right here: Search through the array of guesses, make it 26 characters to represent the alphabet
        //if the user guess equals an already guessed letter, add to try counter. If it's correct, then reveal the letter that is 
        //correct and do it again without adding to the try counter. 
        System.out.print("\nWhat is your guess? ");
        guess = console.nextLine();
        finalGuess = guess.charAt(0);
        guesses[totalTries - 1] = finalGuess; //Puts finalGuess into the array

            for (int i = 0; i < totalTries; i++) { //checks to see if the letter is already guessed
                if (guesses[i] != finalGuess) {
                    System.out.println(guesses[i]);
                    for (int j = 0; i < length; j++) { //scans each letter of random word
                        if (finalGuess == randomWord.charAt(j)) {
                            //put a method that swaps out dashes with the guessed letter
                            totalTries++;
                        }
                    }
                } else {
                    System.out.println("Letter already guessed, try again! ");
                }
            }
        }
    }

我得到这样的输出:

* * * * * * * * * * * * * * *
*    Welcome to Hangman!    *
* * * * * * * * * * * * * * *
You get 10 tries to guess the word by entering in letters!

ostracization
Try #1
Word: -------------
What is your guess? a
Letter already guessed, try again! 
Try #1
Word: -------------
What is your guess? 

这只是说当数组中有一个空元素时,已经猜到了字母.我在这里想念什么吗?

It's just saying that the letter is already guessed when there is an empty element in the array. Am I missing something here?

推荐答案

让我们看一下示例代码(强烈建议您使用调试器自己完成代码):

Let's walk through the code with your example (I strongly suggest you do this yourself with a debugger):

guesses[totalTries - 1] = finalGuess; // guesses[0] = 'a'
if (guesses[i] != finalGuess) // i = 0, guesses[0] = 'a', finalGuess = 'a'
else System.out.println("Letter already guessed, try again! ");

你可以移动

guesses[totalTries - 1] = finalGuess; //Puts finalGuess into the array

最外面的for循环的结尾处的

.处理猜测之前,无需存储猜测.

at the end of the outermost for loop. There's no need to store a guess before having processed it.

这篇关于Java-制作子手游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆