gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:51:22 26 4
gpt4 key购买 nike

我一直在为一个学校项目使用 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() 函数时,它都会增加计数(因为它是一个类变量),最终会增加“_”的数量。

现在针对您当前的双 * 问题,在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com