java - 实现 java Hangman 时无法返回具有正确字母的空白(例如 "_ _ r _")

标签 java bluej

我一直在为一个学校项目使用 Java BlueJ 开发一个基于文本的刽子手游戏,并且大部分都取得了成功,但是我在这部分遇到了一些困难。

我试图做到这一点,以便每次玩家正确猜测一个字母时,它都会打印出插入正确字母的空格(星号)。现在,我让它打印出你猜的字母,并用正确的空格数跳过正确的猜测。 我现在唯一的问题是它在游戏开始时打印的空白数量不正确。

如何解决此空格问题?

import java.util.*;

public class Hangman {
    Word w = new Word();
    private String word = w.chooseWord();
    private int count = 0;
    String guess;

public String getWord() {
    String w = word;
    return w;
}

public int countLetters() {
    for (int i = 0; i < word.length(); i++) {
        if (Character.isLetter(word.charAt(i))) {
            count++;
        }
    }
    return count;
}

public String Blanks() {
    countLetters();
    int num = 0;
    String spaces = "";
    String blankSpace = "*";
    while (num < count) {
        spaces = spaces + blankSpace;
        num++;
    }
    return spaces;
}

String wordSoFar = Blanks();

public int countOccurrences() {
    int count = 0;
    for (int i = 0; i < word.length(); i++) {
        if (word.substring(i, i + 1).equals(guess)) {
            count++;
        }
    }
    return count;
}

public String wordSoFar() {
    for (int i = 0; i < word.length(); i++) {
        if (word.substring(i, i + 1).equals(guess)) {
            wordSoFar = wordSoFar.substring(0, i) + guess + wordSoFar.substring(i + 1 , wordSoFar.length());
        }
    }
    return wordSoFar;
}  

public void Guess() {
    //Removed code that draws hangman due to it making this really long

    boolean correct = false;
    Scanner scan = new Scanner(System.in);
    int numIncorrectGuesses = 0;
    int numCorrectGuesses = 0;

    while (numCorrectGuesses != word.length() && numIncorrectGuesses < 6) {  
        guess = scan.next().substring(0,1);

        if (word.contains(guess)) {
            correct = true;
            numCorrectGuesses += countOccurrences();
            System.out.println(wordSoFar());
        } else {
            correct = false;
            numIncorrectGuesses++;
        }

    //Removed code that draws hangman due to it making this really long

    if (numCorrectGuesses == word.length()) {
        System.out.println("You win");
    } else {
        System.out.println("You lose");
    }
}

添加驱动程序类:

public class runGame {
    public static void main(String[]args) {
        Hangman game = new Hangman();
        System.out.println(game.getWord());
        System.out.println(game.Blanks());
        game.Guess();
    }
}

选择单词的类:

import java.util.*;

    public class Word {
        //String[] bank = {removing word bank due to length};

        public String chooseWord() {
            Random r = new Random();
            return new String(bank[r.nextInt(bank.length)]);
        }
    }

以下是发生的疯狂情况的示例(第一行仅用于测试;最终游戏不会显示该单词。单字符行是我的猜测):

object
************
    _______
   |/      |
   |
   |
   |
   |
   |
___|___
o
o*****
b
ob****
j
obj***
e
obje**
c
objec*
t
object
You win

最佳答案

你必须删除该行

wordSoFar = Blanks();

从 wordSoFar() 函数的开头。而是在guess() 函数的开头执行此操作。每次你猜出一个新字符时,你都会将其初始化为空白。每次调用 Blank() 函数时,它都会增加计数(因为它是一个类变量),最终会增加“_”的数量。

现在,针对当前的 double * 问题,在 Hangman 类中为 wordSoFar 编写一个 getter。

public String getWordSoFar() {
    return wordSoFar
}

在主函数中

public class runGame {
    public static void main(String[]args) {
        Hangman game = new Hangman();
        System.out.println(game.getWord());
        System.out.println(game.getWordSoFar());
        game.Guess();
    }
}

另一种想法是,countLetter()函数应该在计数之前将计数初始化为0。

public int countLetters() {
    int count = 0;
    for (int i = 0; i < word.length(); i++) {
        if (Character.isLetter(word.charAt(i))) {
            count++;
        }
    }
    return count;
}

关于java - 实现 java Hangman 时无法返回具有正确字母的空白(例如 "_ _ r _"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585926/

相关文章:

java - bluej 中的纸牌游戏

java - 在 Java 中运行程序时输出未完全显示

Java Swing动画重影问题

JavaFX 缩放打印弄乱了我的打印机

java - 通过名称获取 Swing 组件

java - 一般错误 : type . .. 不适用于参数

java - 类中的构造函数不能应用于给定类型。希望得到帮助

java - 如何使用外部参数更改数组中元素的值?

java - 在日志记录期间屏蔽 Java 中的某些字符串属性?

java - 在 Tomcat 8 上配置 SSL 并连接超时