【问题标题】:Hangman Java Game Printing Both Wrong and Correct GuessesHangman Java 游戏打印错误和正确的猜测
【发布时间】:2016-12-18 21:10:57
【问题描述】:

我正在使用我的第一种编程语言 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;

      }

    }

}

【问题讨论】:

    标签: java arrays loops for-loop while-loop


    【解决方案1】:

    这段代码:

    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 为真,循环也会继续检查每个字符。只有有人猜对了最后一个字符,它才会起作用。所以我所做的是添加一个break 语句,这样只要letterFound 为真,for 循环就会退出。

    【讨论】:

    • 嗨。感谢回复。我添加了大括号,但是当输入不正确的字母时它没有打印“错误”,而是要求用户输入另一个字母。问候,杰森。
    • @icewolf461:这很糟糕 Java:if (foo = false) { 因为 foo 将被分配为假总是。而是做if (!foo) {
    • 除非你故意总是将solved设置为false,否则你的修复也有错误的操作符。
    • 它给出了一个“已解决”没有初始化的错误,我是默认设置为true还是false?
    • @icewolf461 布尔值在初始化时应始终为false
    【解决方案2】:

    注意在java中,这个分支永远不会被使用:

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

    请改成

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

    我想让你看看herehere too

    【讨论】:

      猜你喜欢
      • 2019-07-17
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多