gpt4 book ai didi

Java Hangman - 错误检查

转载 作者:行者123 更新时间:2023-12-01 15:03:17 26 4
gpt4 key购买 nike

对于我的刽子手游戏,我希望有一堆错误消息来检查是否输入了多个字母、两次猜测同一个字母等。到目前为止我的完整代码:

    import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

public class MainFrame extends javax.swing.JFrame {

public MainFrame() {
initComponents();
}
//declare variables
static String secretWord = "";
double result = 0;
StringBuilder mainWord = new StringBuilder();
StringBuilder xletters = new StringBuilder(); // letters guessed
String[] words = {"technology", "computer", "camera", "graphic", "digital", "media", "technician",
"photography", "troubleshoot", "pixels", "application", "download"};
Random r = new Random();
int randValue = r.nextInt(12);
String guessWord = words[randValue];
int errors = 0;
public static int wins = 0, losses = 0;
String foundWord = null;
private void GuessButtonActionPerformed(java.awt.event.ActionEvent evt) {
String strGuess = GuessText.getText(); //user input
String letter = strGuess;
xletters.append(strGuess.toUpperCase());
String GuessedLetters = xletters.toString();

try {
//replace underscores with letters as they are guessed
do {
for (int i = 0; i < 1; i++) {
secretWord = secretWord + letter.charAt(0);
foundWord = words[randValue].replaceAll("[^" + secretWord + "]", "_ ");
//if user entered more than one letter
if (strGuess.length() > 1) {
JOptionPane.showMessageDialog(null, "Only one letter at a time!");
xletters.append("");
GuessedLetters = null;
GuessText.setText(null);
GuessText.requestFocusInWindow();
} //if letter isn't in word
else if (guessWord.indexOf(strGuess) == -1) {
JOptionPane.showMessageDialog(null, "Sorry, that wasn't in the word.");
errors++;
if (errors == 1) {
Hangman0.setVisible(false);
}
if (errors == 2) {
Hangman1.setVisible(false);
}
if (errors == 3) {
Hangman2.setVisible(false);
}
if (errors == 4) {
Hangman3.setVisible(false);
}
if (errors == 5) {
Hangman4.setVisible(false);
}
if (errors == 6) {
Hangman5.setVisible(false);
}
if (errors == 7) {
Hangman6.setVisible(false);
}
if (errors == 8) {
Hangman7.setVisible(false);
}
if (errors == 9) {
Hangman8.setVisible(false);
}
if (errors == 10) {
Hangman9.setVisible(false);
JOptionPane.showMessageDialog(null, "You lost! The word was: " + guessWord);
losses++;
DirectionsFrame DFrame = new DirectionsFrame();
DFrame.setVisible(true);
setVisible(false);
MainFrame MFrame = new MainFrame();
MFrame.dispose();
xletters.delete(0, 100);
secretWord = "";
foundWord = null;
strGuess = null;
String strLosses = Integer.toString(losses);
String strWin = Integer.toString(wins);
DirectionsFrame.WinsLabel.setText(strWin);
DirectionsFrame.LossesLabel.setText(strLosses);
}
}
}
WordLabel.setText(foundWord.toUpperCase());
GuessedLabel.setText(GuessedLetters);
GuessText.setText(null);
GuessText.requestFocusInWindow();
} while (foundWord == null);
if (foundWord.equalsIgnoreCase(guessWord)) {
JOptionPane.showMessageDialog(null, "Yay!");
wins++;
DirectionsFrame DFrame = new DirectionsFrame();
DFrame.setVisible(true);
setVisible(false);
MainFrame MFrame = new MainFrame();
MFrame.dispose();
xletters.delete(0, 100);
secretWord = "";
foundWord = null;
String strWin = Integer.toString(wins);
String strLosses = Integer.toString(losses);
DirectionsFrame.WinsLabel.setText(strWin);
DirectionsFrame.LossesLabel.setText(strLosses);
}
} catch (StringIndexOutOfBoundsException e) {
JOptionPane.showMessageDialog(null, "Please enter a letter.");
GuessedLabel.setText(GuessedLetters);
GuessText.setText(null);
GuessText.requestFocusInWindow();
}
}


private void GetButtonActionPerformed(java.awt.event.ActionEvent evt) {
//print out underscores to begin game
for (int i = 0; i < guessWord.length(); i++) {
mainWord.append("_ ");
}
String SetMain = mainWord.toString();
mainWord.append(secretWord);
WordLabel.setText(SetMain);
GuessButton.setEnabled(true);
GetButton.setEnabled(false);
}

我遇到的最大麻烦是检查用户是否输入了相同的字母两次。所以你只能猜一次字母表中的一个字母。另外,我认为检查用户是否输入多个字母的代码有一个错误,因为我希望它不将这些字母添加到猜测的字母框中,但它无论如何都会添加。 (即用户猜测“hf”,并且在猜测的字母中出现“hf”,其中应该什么都没有)

我觉得答案就在方法indexOf()中,但我不完全确定if条件会说什么...

我正在使用 Netbeans IDE 7.2 来编译我的代码。请帮忙!谢谢。

最佳答案

在您想知道某些东西是否已被使用的情况下,Set到了手边。例如,在刽子手游戏中使用的字母,您将有 Set<String> Collection :

Set<String> lettersUsed = new HashSet<String>();

如果您想知道是否已选择某些内容:

String inputLetter = "Z";  // test for the letter Z

if (!lettersUsed.contains(inputLetter)) {
lettersUsed.add(inputLetter);

// process letter in word
} else {
// letter has already been used!
}

如果您只想允许初始设置的一组特定字母:

if (lettersAllowed.contains(inputLetter)) {
lettersAllowed.remove(inputLetter);

// process letter in word
} else {
// letter is not allowed (anymore)
}

关于Java Hangman - 错误检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326264/

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