有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的刽子手游戏不会退出循环

我已经为我的一个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();
    }
 
}

共 (1) 个答案

  1. # 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是一种面向对象的语言,所以最好学习它