我想創建一個hang子手程序。他們紛紛猜測這句話是「壞頭髮的一天」,他們看到「* * *」。當用戶輸入一個字符時不會改變。我不是100%確定我錯了,但可能是在passwordlabel2或循環中的某個地方。試圖做一個hang子手程序,不能讓它顯示猜對的字母
演示類
public class SecretPhrase {
int wrong = 0; //ignore for now
String phrase = "Bad hair day"; //hidden, what the user has to guess
String hiddenPhrase = "*** **** ***"; //what the user originally sees
public void changeLetter(char input)
{
StringBuilder checker = new StringBuilder(input);
StringBuilder(hiddenPhrase);
boolean wrongGuess = true;
for (int i=0; i<phrase.length(); i++)
{
if (phrase.charAt(i) == input){
checker.setCharAt(i, input);
wrongGuess = false;
}
}
hiddenPhrase = checker.toString();
if (wrongGuess){
wrong++;
}
}
private void StringBuilder(String hiddenPhrase) {
// TODO Auto-generated method stub
}
}
UI類
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class SecretPhraseUI extends JApplet implements ActionListener {
SecretPhrase phrase = new SecretPhrase();
JLabel passwordLabel = new JLabel("Enter a letter to guess the phrase." ); //sets label to display message
JLabel passwordLabel2 = new JLabel(phrase.hiddenPhrase ); //sets label to display message
JTextField inputBox = new JTextField(40); //sets text field
JButton runButton = new JButton("Run"); //button that starts program
Container con = getContentPane(); //gets container
public void init()
{
con.setLayout(new FlowLayout());//sets flowlayout
con.add(new JLabel()); //jlabel container
con.add(inputBox); //input box container
con.add(runButton); //run button container
con.add(passwordLabel); //password label container
con.add(passwordLabel2); //password label container
runButton.addActionListener(this);//looks to see if run is clicked
inputBox.addActionListener(this);//looks to see if input box is used
}
public void actionPerformed(ActionEvent e)
{
String userInput = inputBox.getText(); //gets input from user
}
}
沒有地方,你嘗試更新了'passwordLabel2'標籤。您每次嘗試致電changeLetter時都不會調用'changeLetter()' – John3136
我不斷收到錯誤。 – Marcus