2013-11-25 13 views
1

我想創建一個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 





    } 
    } 
+1

沒有地方,你嘗試更新了'passwordLabel2'標籤。您每次嘗試致電changeLetter時都不會調用'changeLetter()' – John3136

+0

我不斷收到錯誤。 – Marcus

回答

1

你必須要label.setText(「要顯示你想要的文字後行動」)。所以當你檢查字符時,如果字符猜對了,那麼就執行passwordLabel2.setText(phrase.hiddenPhrase)。 :)

工作實例

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) { 
     if (!inputBox.getText().isEmpty()) { 
      phrase.changeLetter(
        inputBox.getText().charAt(0)); //gets input from user 
      passwordLabel2.setText(phrase.hiddenPhrase); 
     } 
    } 
} 


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(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++; 
     } 
    } 
} 
+0

非常感謝!有一件事我很困惑。當我創建了stringbuilder時,它給了我一個錯誤,除非我把void stringbuilder方法放入,否則它不會消失。你怎麼不需要它來使它工作? – Marcus

+1

「StringBuilder(hiddenPhrase);」行被認爲是對此方法StringBuilder的調用。 您PROGRAMM不需要這種方法 私人無效的StringBuilder(字符串hiddenPhrase){// TODO自動生成方法存根 } 所以也不需要調用的StringBuilder(hiddenPhrase)。 – Willmore

1

它看起來並不像你這樣做與用戶輸入任何內容。你可以用用戶輸入做一些事情。將用戶輸入附加到另一個字符串,並使用標籤的setText方法用用戶輸入更新標籤。

相關問題