【问题标题】:Hangman game in Java does not get out of the loopJava 中的刽子手游戏并没有脱离循环
【发布时间】:2021-02-16 16:36:12
【问题描述】:

我为我的一个 Java 课程作业创建了这个刽子手游戏。但是我的代码存在许多问题:

  1. 即使玩家猜对了所有字母,我似乎也无法跳出循环
  2. 我没有进入询问玩家是否要再试一次的部分。
  3. 我想创建一种方法来加载从列表中选择一个单词并将其转换为单个字符,但我不知道该怎么做。 非常感谢您的帮助: 这是我的代码:
import java.util.Scanner;
import java.util.Random;

public class test {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        Random random = new Random();

        String wordList[] = {"AARDVARK", "BANANA", "ORBITAL", "LANDSCAPE","BEACHES", "SCIENCE", "PRODUCTION"};
        
        boolean play = true;
        while(play = true){
            System.out.println("You are playing game of hangman. Can you guess the word? ");
           
            char [] selectedWord = wordList[random.nextInt(wordList.length)].toCharArray(); // java -> j,a,v,a
           
            char [] playerGuess = new char[selectedWord.length]; // "* * * * * * * *"
            
            for(int i=0; i<playerGuess.length; i++){ // Assign empty dashes at start "* * * * * * * *"
                playerGuess[i] =  '*';
                
            } 
            System.out.println(selectedWord);
            boolean correctGuess = true;
            int numberOfGuesses = 0;
            
            while(correctGuess = true ){
                addSpace(playerGuess);
                
                System.out.println("Enter your next choice: ");
                char letterInput = input.nextLine().charAt(0);
                numberOfGuesses = numberOfGuesses + 1 ;
                
                if(letterInput == '*'){
                    correctGuess = true;
                    play = false;
                } else{
                    for(int i=0; i<selectedWord.length; i++){
                        if(selectedWord[i] == letterInput){
                            playerGuess[i] = letterInput;
                        } 
                    } 
                    
                    if(checkGuess(playerGuess)){
                        correctGuess = true;
                        System.out.println("You guessed the word! You guessed " + numberOfGuesses + " times.");
                    }
                }        
            } 
            if (playerGuess.equals(selectedWord))
                break;
            
            System.out.println("Do you want to play again? (yes/no) ");
            String playAgain = input.nextLine();
            if(playAgain.equals("no")){
                play = false;
            }
          
        }
    
        System.out.println("Game Over!");
    }
  
    public static boolean checkGuess(char[] word){
        boolean guessed = true;
        for(int i=0; i < word.length; i++){
            if(word[i] == '*'){
                guessed = false;
            }
        }
        return guessed;
    }
    
    public static void addSpace(char [] word){
        for(int i=0; i < word.length; i++){ // Assign empty dashes at start "* * * * * * * *"
            System.out.print(word[i] + " ");
        } 
        System.out.println();
    }
 
}

【问题讨论】:

  • play = true 这是一个分配,而不是比较。 play == true是比较
  • 谢谢,我把它改成 play == true,但我仍然卡在循环中
  • 答案已更新。

标签: java loops methods


【解决方案1】:

你需要改变

while (play = true) {
while(correctGuess = true ){            

while(play == true) {
while (correctGuess == true) {

或 更好

while(play) {
while(correctGuess) { 

一般来说,当您进行逻辑比较时,您只是在评估表达式是真还是假。因此,如果您已经有 truefalse 值,请直接使用它。

这是从数组中选择单词的方法。当到达最后一个单词时,它将重新开始。

static int nextWord = 0;
static String wordList[] = {"AARDVARK", "BANANA", "ORBITAL", "LANDSCAPE","BEACHES", "SCIENCE", "PRODUCTION"};
public static String getWord() {
    if (nextWord == wordList.length) {
        nextWord = 0;
    }
    return wordList[nextWord++];
}

用法

String word = getWord();

但是,您最好学习使用实例方法,而不要过多地依赖静态方法。它们当然有它们的用途,但 Java 是一种面向对象的语言,所以最好学习它。

【讨论】:

  • 是的,但我仍然被困在循环中。
  • 我将它移到了循环中,但循环仍然不想结束。
  • 对不起,那是我的错误。我刚刚运行了你的程序。不应该将if(checkGuess(playerGuess)){ correctGuess = true;... correctGuess 设置为 false 以便退出该循环吗?
  • 是的,这解决了循环问题。谢谢。
  • 很高兴我能帮上忙。很抱歉之前的错误信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多