Hangman Java游戏在错误和正确的猜测上打印



我正在做我的第一种编程语言Java,并且我很难正确打印正确的猜测。当输入错误的字母时,它会打印"错误!"但是,当猜测正确的字母时,它既打印出"正确!"one_answers"错误!" ...任何帮助都将受到赞赏。谢谢。:)
这些单词是从数组中拉出的。

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!");
               }

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

请注意,在Java中,将永远不会使用此分支:

if (solved = false) {
    wrongGuesses++;
}

请将其更改为

    if (solved == false) {
        wrongGuesses++;
        System.out.println("Wrong!");//And then add the wrong print out here
    }

我希望您在这里和这里看看

最新更新