对于我的刽子手游戏,我希望有一堆错误消息来检查诸如输入的多个字母、两次猜测同一个字母等内容。到目前为止,我的完整代码:
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 来编译我的代码。请帮忙!谢谢。