Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.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,我正在用java创建一个hangman程序。该程序应该随机生成单词,当用户正确猜出一个字母时,它就会显示出来。我创建了一个函数,它根据单词的长度打印下划线的数量。我遇到的问题是,我不确定在猜测时如何用字母替换下划线 public static void main(String[] args) { int choice; String wordSoFar = ""; Console.print("Take your time.&quo

我正在用java创建一个hangman程序。该程序应该随机生成单词,当用户正确猜出一个字母时,它就会显示出来。我创建了一个函数,它根据单词的长度打印下划线的数量。我遇到的问题是,我不确定在猜测时如何用字母替换下划线

public static void main(String[] args) {
    int choice;
    String wordSoFar = "";

        Console.print("Take your time.");
        choice = Console.readInt("Enter your choice, when you're ready : ");

    }

    if (choice == 1) {
        
        for (int i = 0; i < wordLength; i++) {
            word += " _ ";
        }

        do {
            String guess = Console.readString("Enter your guess here : ");
            String response = secert(guess);
            Console.print(response);
        } while (incorrectLetters < maxIncorectLetters || correctLetters < wordLength);
    }
}

public static String secretWordSelector(String secretWord) {
    String[] secretWordsList = { "fun", "boring", "yellow", "phython", "tesla", "iphone", "computer", "adventure",
             };
    int min = 0;
    int max = 8;

    secretWord = secretWordsList[randomNumber(min, max)];

    return secretWord;

}
public static String letterChecker(String guess, String wordSoFar) {
    String response = "";
    String secertWord = "";
    if (secretWord == guess)) {
        int index = secretWord.indexOf(guess);
        correctLetters++;
        answer = "Congratulations!
            
    } else  {
        answer= "That is a incorrect guess! 
    }

    return answer;
}
publicstaticvoidmain(字符串[]args){
智力选择;
字符串wordSoFar=“”;
Console.print(“慢慢来”);
choice=Console.readInt(“准备好后输入您的选择:”;
}
如果(选项==1){
for(int i=0;i
通过以下更改,您的代码可以得到很大改进:

  • 您不需要复杂函数,
    randomNumber
    。您只需将数组的长度传递给,它将在数组-1的
    0
    length\u范围内生成
    int
  • 同样,不需要函数
    length
    。您可以简单地调用以获取字符串的长度
  • 您不必要地将参数传递给函数,
    secretWordSelector
  • 由于您在多个方法中使用了
    字符串,因此通过使其全局化,处理起来会更容易
  • 您在
    letterChecker
    中再次调用了
    secretWordSelector
    ,这是错误的。您必须处理在用户选择选项时随机选择的同一单词
  • 由于运算符错误,您的
    while
    循环是无限的。它应该是
    while(不正确的字母
  • 我已经重新编写了这个方法,
    letterChecker
    ,其中有许多更改是不言自明的。我还提供了一些注释,以帮助您轻松理解其代码
  • 包含这些改进的代码:

    import java.util.Random;
    import java.util.Scanner;
    
    public class Main {
        private static int incorrectLetters;
        private static int correctLetters;
        private static String wordSoFar = "";
    
        public static void main(String[] args) {
            int choice;
    
            Scanner scanner = new Scanner(System.in);
            final int maxIncorectLetters = 6;
            System.out.println("Welcome to Mahmoud's Hangman game!");
            System.out.println("Would you like to play. 1 - Yes. 0 - No");
            System.out.print("Enter your choice: ");
            choice = Integer.parseInt(scanner.nextLine());
    
            if (choice == 0) {
                System.out.println("Take your time.");
                System.out.print("Enter your choice, when you're ready: ");
                choice = Integer.parseInt(scanner.nextLine());
            }
    
            if (choice == 1) {
                System.out.println("Be careful, you can only have 6 wrong guesses");
                int wordLength = 0;
                String secretWord = secretWordSelector();
                wordLength = secretWord.length();
                System.out.println("Your word has " + wordLength + " letters.");
                for (int i = 0; i < wordLength; i++) {
                    wordSoFar += "_";
                }
    
                do {
                    System.out.print("Enter your guess here: ");
                    String guess = scanner.nextLine();
                    String response = letterChecker(guess.toLowerCase().charAt(0), secretWord);
                    System.out.println(response);
                } while (incorrectLetters < maxIncorectLetters && correctLetters < wordLength);
            }
        }
    
        public static String secretWordSelector() {
            String[] secretWordsList = { "geography", "cat", "yesterday", "java", "truck", "opportunity", "fish", "token",
                    "transportation", "bottom", "apple", "cake", "remote", "boots", "terminology", "arm", "cranberry",
                    "tool", "caterpillar", "spoon", "watermelon", "laptop", "toe", "toad", "fundamental", "capitol",
                    "garbage", "anticipate", "pesky" };
    
            return secretWordsList[new Random().nextInt(secretWordsList.length)];
        }
    
        public static String letterChecker(char guess, String secretWord) {
            String response = "";
    
            // Initialize a StringBuilder with wordSoFar
            StringBuilder sb = new StringBuilder(wordSoFar);
    
            if (secretWord.indexOf(guess) != -1) {// i.e. if guess is present in secretWord
                // Replace each corresponding occurrence of '_' in the StringBuilder with guess
                for (int i = 0; i < secretWord.length(); i++) {
                    char ch = secretWord.charAt(i);
                    if (ch == guess) {
                        sb.setCharAt(i, ch);
                        correctLetters++;
                    }
                }
                // Assign the updated StringBuilder to wordSoFar
                wordSoFar = sb.toString();
                return "Congratulations! You have guessed correctly. " + " You have " + incorrectLetters
                        + " incorrect guesses " + wordSoFar;
            }
            incorrectLetters++;
            response = "That is a incorrect guess! " + " You have " + incorrectLetters + " incorrect guesses " + wordSoFar;
    
            return response;
        }
    }
    
                                                                                                                                             
    
    Welcome to Mahmoud's Hangman game!
    Would you like to play. 1 - Yes. 0 - No
    Enter your choice: 1
    Be careful, you can only have 6 wrong guesses
    Your word has 4 letters.
    Enter your guess here: o
    That is a incorrect guess!  You have 1 incorrect guesses ____
    Enter your guess here: e
    That is a incorrect guess!  You have 2 incorrect guesses ____
    Enter your guess here: a
    That is a incorrect guess!  You have 3 incorrect guesses ____
    Enter your guess here: h
    Congratulations! You have guessed correctly.  You have 3 incorrect guesses ___h
    Enter your guess here: f
    Congratulations! You have guessed correctly.  You have 3 incorrect guesses f__h
    Enter your guess here: i
    Congratulations! You have guessed correctly.  You have 3 incorrect guesses fi_h
    Enter your guess here: s
    Congratulations! You have guessed correctly.  You have 3 incorrect guesses fish
    

    看看String.replaceAll(),它可能会派上用场!但我想你离我不远了。先用笔和纸解决它,然后修复java程序!