我在Youtube的指导下使用Java进行Hangman游戏,即使尝试正确猜出单词,尝试次数也会减少。仅当玩家猜错时,我应该在代码中添加些什么以减少尝试次数?

我试过使用布尔型的letterIsGuessed,但我不能将其放在检查正确的布尔值的循环中,因为它还会检查字母是否不在且仍输出错误值的地方。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package project_hangman;

import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author NoSwear
 */
public class Project_Hangman {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    Random random = new Random();
    String[] guesses = {"yoko", "michael", "slovakia", "shimura ken", "yuuta", "sunshine", "shiritori", "yokohama", "kyoto", "programming", "smartphone", "shinzo abe", "katakana", "kaomoji", "iron man", "shogi", "anime", "kendo", "kyudo", "kenjutsu"};

    boolean Playing = true;     // Separates the game over and the game itself

    while (Playing) {
        System.out.println("Welcome to the Hangman game!");
        System.out.println("Developed by NoSwear");

        char[] randomWordGuess = guesses[random.nextInt(guesses.length)].toCharArray();     // Takes a random position of the word in the "guesses" field and grabs it, turns them into char
        int amountOfGuesses = 10;
        char[] playerGuess = new char[amountOfGuesses];

        for (int i = 0; i < playerGuess.length; i++) {
            playerGuess[i] = '_';
        }

        boolean wordIsGuessed = false;      // Whole word guessed
        int tries = 0;      // Goes into println, informs the player about the amount of guesses he/she has made

        while (!wordIsGuessed && tries != amountOfGuesses) {
            System.out.println("Current guesses : ");
            printArray(playerGuess);
            System.out.printf("You have %d tries left.\n", amountOfGuesses - tries);    // See : String conversion
            System.out.println("Enter a single character");
            char input = scanner.nextLine().charAt(0);      // Takes only the first letter in the whole sentence the user will input


            if (input == '-') {
                Playing = false;
                wordIsGuessed = true;
                System.out.println("Thank you for playing");
            } else {
                for (int i = 0; i < randomWordGuess.length; i++) {
                    if (randomWordGuess[i] == input) {
                        playerGuess[i] = input;
                    }
                }

                tries++;


                if (isTheWordGuessed(playerGuess)) {
                    wordIsGuessed = true;
                    System.out.println("Congratulations, you won the game!");
                }
            }
        }
        if (!wordIsGuessed) System.out.println("You ran out of guesses :(");
        System.out.println("Do you want to play another game? (yes/no)");
        String anotherGame = scanner.nextLine();
        if (anotherGame.equals("no")) Playing = false;
    }
    System.out.println("Game over");

}

public static void printArray(char[] array) {       // Prints the current state of the word guess
    for (int i = 0; i < array.length; i++) {
        System.out.print(array[i] + " ");
    }
    System.out.println();
}

public static boolean isTheWordGuessed(char[] array) {      // Checks if all empty spaces are filled
    for (int i = 0; i < array.length; i++) {
        if (array[i] == '_') return false;
    }
    return true;
}


}


猜出正确的字母后:

(例)
预期结果=


  当前猜测:
  _ _ _ a _ a _
  您还有10次尝试机会。
  输入一个字符


实际结果=


  当前猜测:
  _ _ _ a _ a _
  您还有9次尝试机会。
  输入一个字符

最佳答案

看一下这段代码

        } else {
            for (int i = 0; i < randomWordGuess.length; i++) {
                if (randomWordGuess[i] == input) {
                    playerGuess[i] = input;
                }
            }

            tries++;


您需要向tries添加一个条件,但是在每次迭代中都增加tries

声明boolean isMatch=false;并在if语句中使用此变量以知道您的控件具有正匹配项

        } else {
            isMatch = false;//-------------------------------------------
            for (int i = 0; i < randomWordGuess.length; i++) {
                if (randomWordGuess[i] == input) {
                    isMatch=true;//--------------------------------------
                    playerGuess[i] = input;
                }
            }
            if(!isMatch)//------------------------------------------------
                tries++;


还要确保在主循环的每次迭代之后或之前,为isMatch分配一个false值。

09-10 13:44
查看更多