基本的Java Hangman [英] Basic Java Hangman

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

问题描述

大家好,我刚刚开始学习Java作为我的第一门编程语言! 在课堂上,我们被分配使用while和for循环制作一个基本的Hangman游戏!

Hey guys I am just starting to learn Java as my first programming language! In class we were assigned to make a basic Hangman game with the use of a while and for loops!

我到目前为止有什么

当用户输入第一个猜测时,它确实会识别出他/她猜测的字符已得到纠正,但是继续并指出我猜错了字母!

When the user inputs the first guess it does recognize that that characters that he/she guessed was corrected but just continues on and stating that I have guessed an incorrect letter!

非常感谢您的帮助!!我的问题是我的代码在做什么错?我需要该程序告诉用户他的猜测是对还是错.

Help would be very appreciated!! My question is what am I doing wrong in my code? I need the program to tell the user if his guess is right or wrong.

我的代码:

import cs1.Keyboard;

public class Hangman {
    public static void main(String args[]) {
        int guessCount = 0;
        int correctGuess = 0;
        boolean foundIt;
        boolean solved;
        char guess, answer;
        String word;

        System.out.println("Welcome to HangMan!");
        System.out.println("Please enter a word for the opponent to guess!");
        word = Keyboard.readString();
        while (guessCount <= 6) {
            System.out.println("Please enter any letter A-Z as your guess!");
            guess = Keyboard.readChar();
            for (int i = 0; i < word.length(); i++) {
                if (guess == word.charAt(i)) {
                    System.out.println("You have guessed a correct letter!");
                    correctGuess++;
                    System.out.println("Correct Guess Count: "
                                        + correctGuess);
                    solved = false;
                }
                else if (guess != word.charAt(i)) {
                    System.out.println("Sorry! That is an incorrect guess! " 
                                       + "Please try again!");
                    guessCount++;
                    System.out.println("Guess Count: " + guessCount);
                    solved = false;
                }
            }
            if (correctGuess == word.length()) {
                solved = true;
                System.out.println("Congratulations! " + 
                                   "You have guessed the word!");
            }                   
        }
    }
}

这是我到目前为止的内容,这是输出

This is what I have so far and here is the output

Welcome to HangMan!
Please enter a word for the opponent to guess!
hello
Please enter any letter A-Z as your guess!
l
Sorry! That is an incorrect guess! Please try again!
Guess Count: 1
Sorry! That is an incorrect guess! Please try again!
Guess Count: 2
You have guessed a correct letter!
Correct Guess Count: 1
You have guessed a correct letter!
Correct Guess Count: 2
Sorry! That is an incorrect guess! Please try again!
Guess Count: 3
Please enter any letter A-Z as your guess!

推荐答案

您将guessString中的每个字符进行比较,然后为每个字符显示消息 .相反,您应该编写一个返回与输入匹配的字符数的方法(该方法还可以处理具有重复字母的单词).所以,

You compare the guess to every character in the String and then display the message for every character. Instead, you should write a method that returns a count of characters that match the input (this also handles words that have repeats of letters). So,

private static int countOf(String in, char ch) {
    int count = 0;
    for (char c : in.toCharArray()) {
        if (c == ch) {
            count++;
        }
    }
    return count;
}

然后您可以这样称呼它,

Then you can call it like,

guess = Keyboard.readChar();
int count = countOf(word, guess);
if (count > 0) {
  System.out.println("You have guessed a correct letter!");
  correctGuess += count;
} else {
  System.out.println("Sorry! That is an inncorrect guess! Please try again!");
}
guessCount++;

编辑要在没有第二种方法的情况下进行操作,

Edit To do it without a second method you could use,

guess = Keyboard.readChar();
int count = 0;
for (char c : in.toCharArray()) {
    if (c == guess) {
        count++;
    }
}
if (count > 0) {
  System.out.println("You have guessed a correct letter!");
  correctGuess += count;
} else {
  System.out.println("Sorry! That is an inncorrect guess! Please try again!");
}
guessCount++;

而且,由于您没有使用 for-each -

And, since you haven't used the for-each -

char[] chars = in.toCharArray();
for (int i = 0; i < chars.length; i++) {
    if (chars[i] == guess) {
        count++;
    }
}

这篇关于基本的Java Hangman的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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