试图制作一个刽子手程序,不能让它显示猜测的字母

时间:2013-11-25 05:22:55

标签: java

我正在尝试创建一个刽子手程序。他们必须猜测的短语是“糟糕的发型日”,他们看到“ * * * *”。当用户输入一个字符时没有任何变化。我不是百分之百确定我是否会出错,但可能是在密码标签2或循环中的某个地方。

演示班

 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





    }
    }

2 个答案:

答案 0 :(得分:1)

看起来你没有对用户输入做过任何事情。您可以使用用户输入执行某些操作。将用户输入附加到另一个String并使用标签的setText方法使用用户输入更新标签。

答案 1 :(得分:1)

你必须制作label.setText(“你希望在行动后显示的文字”)。因此,当您检查char时,如果猜对了,则执行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++;
         }
    }
}