java hangman游戏,基本的Java 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!

What I have so far

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.

My code:

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!

解决方案

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++;

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++;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值