gpt4 book ai didi

java - 为什么这段代码不起作用?刽子手

转载 作者:行者123 更新时间:2023-11-30 07:13:26 25 4
gpt4 key购买 nike

我正在制作刽子手游戏。一切正常,我已经准备好用于使游戏失败并为猜测提供 -1 的代码。虽然将它添加到 else 语句时它会重复等于单词的长度并且它也会给出一个猜测——即使它是正确的?我看不出代码有什么问题?我相信这是我的代码,但我猜错了,但没有放对,尽管我没有其他办法?

这是代码:

private class check implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
// Grabs the letter from the guessField and converts it into a char
// which can be used to compare against the word.
guess = guessField.getText();
guessField.setText("");
char guess2 = guess.charAt(0);

// --------------------
// Here is the guessing logic but it's currently
// not working and you can not win since i haven't written code for
// it yet. it's not selecting all the letters. for Example if
// choosing A in a word such as Banana it only selects the first
// a--------------------------- //
String displaySecret = wordField.getText();
if (displaySecret.equals("")) {/* case for fist execution */
displaySecret = "";
for (int i = 0; i < random.length(); i++)
displaySecret += "_ ";
}
String newDisplaySecret = "";
for (int v = 0; v < random.length(); v++) {
if (guess2 == random.charAt(v)) {
newDisplaySecret += random.charAt(v); // newly guessed
// character
} else {
newDisplaySecret += displaySecret.charAt(v); // old state
guesses--;
statusLabel.setText("Guesses left: " + guesses);
missField.setText(missField.getText() + guess);
if (guesses <= 0) {
JOptionPane.showMessageDialog(null,
"Game over! The word was: " + random);
guessField.setEditable(false);
wordField.setText("");
missField.setText("");
guesses = 7;
statusLabel.setText("Guesses left: " + guesses);
}
}
}
displaySecret = new String(newDisplaySecret);
wordField.setText(displaySecret);
if (displaySecret.equals(random)) {
JOptionPane.showMessageDialog(null, "You Won! The Word was: "
+ random);
guesses = 7;
statusLabel.setText("Guesses left: " + guesses);
wordField.setText("");
missField.setText("");
guessField.setEditable(false);
}
} catch (Exception e) {
System.out.println(e);
}
}
}

最佳答案

如果 random 是您的单词,您将遍历它的每个字符,然后检查每个字符是否与您对每个与猜测 a -1 不匹配的字符的猜测相匹配。

例如:单词是 Bananarama,您猜 n 您的第一个和第二个匹配项将转到 else 子句。然后 if 子句再次出现一次,然后是 else 等等。

你必须

  1. 遍历所有字符,检查它们是否匹配
  2. 如果发生匹配,替换字符并增加计数器
  3. 检查正确字符的计数器是否等于0
  4. 如果是,减少猜测

一些其他提示:在比较之前,您应该在输入和单词字符串上使用 .toLower() 以允许不区分大小写

一些示例代码:

int charsGuessedCorrectly;
for ( int i = 0; i < random.length( ); i++ )
{
if ( random.charAt( i ) == guess )
{
charsGuessedCorrectly++;
newDisplaySecret += random.charAt(v); // newly guessed
// character
}
}

if ( charsGuessedCorrectly == 0 )
{
newDisplaySecret += displaySecret.charAt(v); // old state
guesses--;
statusLabel.setText("Guesses left: " + guesses);
missField.setText(missField.getText() + guess);
if (guesses <= 0) {
JOptionPane.showMessageDialog(null,
"Game over! The word was: " + random);
guessField.setEditable(false);
wordField.setText("");
missField.setText("");
guesses = 7;
statusLabel.setText("Guesses left: " + guesses);
}

关于java - 为什么这段代码不起作用?刽子手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19437734/

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