Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 只有在猜错的情况下,如何减少刽子手游戏中的尝试次数?_Java - Fatal编程技术网

Java 只有在猜错的情况下,如何减少刽子手游戏中的尝试次数?

Java 只有在猜错的情况下,如何减少刽子手游戏中的尝试次数?,java,Java,我在Youtube的指导下用Java制作了一个刽子手游戏,即使玩家猜对了单词,尝试次数也会减少。我应该在代码中添加什么来减少只有在玩家猜错的时候才尝试的次数 我曾尝试使用布尔字母IsGuess,但我不能将其放入一个循环中检查正确的字母,因为它也会检查字母不在的位置,并且仍然输出一个假值 /* * To change this license header, choose License Headers in Project Properties. * To change this templ

我在Youtube的指导下用Java制作了一个刽子手游戏,即使玩家猜对了单词,尝试次数也会减少。我应该在代码中添加什么来减少只有在玩家猜错的时候才尝试的次数

我曾尝试使用布尔字母IsGuess,但我不能将其放入一个循环中检查正确的字母,因为它也会检查字母不在的位置,并且仍然输出一个假值

/*
 * 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;
}


}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装工程(u hangman);;
导入java.util.Random;
导入java.util.Scanner;
/**
*
*@author NoSwear
*/
公共类项目{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
随机=新随机();
字符串[]猜测={“yoko”、“michael”、“Slovia”、“shimura ken”、“yuuta”、“sunshine”、“shiritori”、“yokohama”、“kyoto”、“programming”、“smartphone”、“shinzo abe”、“katakana”、“kaomoji”、“iron man”、“shogi”、“anime”、“kendo”、“kyudo”、“kenjutsu”};
boolean Playing=true;//将游戏和游戏本身分开
玩的时候{
System.out.println(“欢迎来到刽子手游戏!”);
System.out.println(“由NoSwear开发”);
char[]randomWordGuess=guesss[random.nextInt(guesss.length)].toCharArray();//在“guesses”字段中获取单词的随机位置,并将其转换为char
int amountofguesss=10;
char[]playerGuess=新字符[amountofguesss];
for(int i=0;i
猜出正确的字母后:

(示例) 预期结果=

当前猜测:
__uua ua ua
您还有10次尝试。
输入单个字符

实际结果=

当前猜测:
__uua ua ua
您还有9次尝试。
输入单个字符


您需要记住在循环中找到字符匹配的时间:

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

您需要记住在循环中找到字符匹配的时间:

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

看看这部分代码

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

            tries++;  

还要确保在主循环的每次迭代之后或之前,都使用
false
值分配
isMatch

查看这部分代码

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

            tries++;  

另外,请确保在主循环每次迭代之后或之前,使用
false
值分配
isMatch

指定此程序应获取的输入和输出此程序应正确打印。指定此程序应获取的输入和输出此程序应正确打印。您现在只计算成功次数l回答。这与OP想要的正好相反。@Tom哦,我的错。在这里把变量搞混了,一般来说,人们会想在这样的游戏中减少
numberOfChances
。非常感谢!:)我尝试了一下这里的东西,但它不起作用,所以我对它做了一些修改,现在它工作得很好!感谢you@NoSwearsk请分享你所做的。你现在只计算成功的答案。与OP想要的正好相反。@Tom哦,我的错。在这里混淆了变量的使用,一般来说,人们会考虑在这样的游戏中减少<代码>机会数。非常感谢!:)我尝试了一下这里的东西,但它不起作用,所以我对它做了一点修改,现在它工作得很好!感谢you@NoSwearsk请分享你所做的。