gpt4 book ai didi

java - Hangman Java 游戏打印错误和正确的猜测

转载 作者:行者123 更新时间:2023-12-02 03:09:50 26 4
gpt4 key购买 nike

我正在使用我的第一种编程语言 Java,但我无法正确猜测以正确打印。当输入错误的字母时,它会打印“错误!”但是当猜到正确的字母时,它会打印“正确!”和“错误!”...任何帮助表示赞赏。谢谢。 :)
这些单词是从数组中提取的。

import java.util.Scanner;
import java.util.Random;
public static void main(String[] args) {
System.out.println("Welcome to Hangman! Below you can find the rules of the game.");
//Welcome message
System.out.println();
//Spacing
System.out.println("How to play:");
System.out.println();
//Spacing
System.out.println("Hangman is a word guessing game. Guess the word by suggesting letters within a certain number of guesses.");
System.out.println("The word to guess is represented by a row of dashes, representing each letter of the word.");
System.out.println("If you guess a letter correctly, it replaces a dash and you can continue to guess until the word is uncovered.");
System.out.println("However, if you guess wrong you lose a try. You have a maximum of 5 tries. After guessing 10 words, you win! Good luck!");
//Rules of game
System.out.println();
//Spacing
Scanner in = new Scanner (System.in);
//Scanner created
String[] intro = {"after", "think", "could", "thank", "round", "clump", "plane", "beach", "towel", "tiger", "goat", "monkey", "house"};
String[] beginner = {"around", "amount", "grown", "focus", "cedar", "flute", "unicorn", "fruit", "basket", "burger", "chips", "juice"};
String[] intermediate = {"about", "bring", "clean", "mother", "locker", "hunter", "drink", "eight", "spray", "untie", "cents"};
String name;
int age;
//Variables initialized
System.out.println("Please enter your name:");
//Prompt for name
name = in.next();
//Input for name
System.out.println("Please enter your age:");
//Prompt for age
age = in.nextInt();
//Input for age

if (age >= 4 && age < 7){
System.out.println("You have been placed in the Intro level!");
Random rand = new Random();
//Random created
int randomWord = rand.nextInt(12)+0;
//Stores the position of array
String word2guess = intro[randomWord];
//String is the value stored in the generated position of array
char[] word2Char = word2guess.toCharArray();
//Stores the word in a char array
char[] wordLength = new char [word2Char.length];
//Creates an array for word length
for (int i = 0; i < word2Char.length; i++){
wordLength[i] = '_';
}
//Replaces each letter of the word with an underscore
for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
//Prints the word in underscore format with a space between each letter
int wrongGuesses = 0;
int correctGuesses = 0;
boolean letterFound = false;
boolean nextWord = true;
while (nextWord){
while(correctGuesses != word2Char.length){
System.out.println();
//Spacing
System.out.println("Please enter a letter:");
char guess = in.next(".").charAt(0);
//Input for only ONE letter
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false && wrongGuesses <= 4){
wrongGuesses++;
System.out.println("Wrong! You have "+ (5 - wrongGuesses)+" attempts remaining. Please try again.");
}else{
correctGuesses++;
System.out.println("Correct!");
}


for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
System.out.println();
//Spacing
if (correctGuesses == word2Char.length){
System.out.println("Congratulations! You guessed the word. The word was: "+word2guess);
nextWord = true;
}
if (wrongGuesses == 5){
System.out.println();
//Spacing
System.out.println("Game Over! You have no attempts remaining. The word was: "+word2guess);
nextWord = false;
break;

}

}

}

最佳答案

这一段代码:

for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
correctGuesses++;
System.out.println("Correct!");
}


}
if(!letterFound){
wrongGuesses++;
System.out.println("Wrong!");
}

需要是这样的:

for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false){
wrongGuesses++;
System.out.println("Wrong!");
}else{
correctGuesses++;
System.out.println("Correct!");
}

问题是在 for 循环中,即使 letterFound 为 true,循环也会继续检查每个字符。只有当有人正确猜出最后一个字符时它才会起作用。因此,我所做的是添加一个 break 语句,以便每当 letterFound 为 true 时,for 循环就会退出。

关于java - Hangman Java 游戏打印错误和正确的猜测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41212928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com