Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图制作一个刽子手程序,可以';我不能让它显示猜出来的字母_Java - Fatal编程技术网

Java 试图制作一个刽子手程序,可以';我不能让它显示猜出来的字母

Java 试图制作一个刽子手程序,可以';我不能让它显示猜出来的字母,java,Java,我正在尝试创建一个刽子手程序。他们不得不猜测的短语是“坏头发日”,他们看到“****”。当用户输入字符时,没有任何变化。我不能100%确定我是否出错,但可能是在密码标签2或循环中的某个地方 演示课 public class SecretPhrase { int wrong = 0; //ignore for now String phrase = "Bad hair day"; //hidden, what the user has to guess String hi

我正在尝试创建一个刽子手程序。他们不得不猜测的短语是“坏头发日”,他们看到“****”。当用户输入字符时,没有任何变化。我不能100%确定我是否出错,但可能是在密码标签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

    }

    }

看起来您没有对用户输入做任何操作。您可以使用用户输入做一些事情。将用户输入附加到另一个字符串,并使用标签的setText方法使用用户输入更新标签。

必须使用label.setText(“操作后要显示的文本”)。因此,当您检查字符时,如果字符猜对了,请执行passwordLabel2.setText(phrase.hiddenprase)

工作示例

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++;
         }
    }
}
导入java.awt.Container;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JApplet;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
公共类秘密用语
扩展JApplet
实现ActionListener{
SecretPhrase=新SecretPhrase();
JLabel passwordLabel=new JLabel(“输入一个字母来猜测短语。”);//设置标签以显示消息
JLabel passwordLabel2=新JLabel(
phrase.hiddenprase);//设置标签以显示消息
JTextField inputBox=新的JTextField(40);//设置文本字段
JButton runButton=newjbutton(“Run”);//启动程序的按钮
Container con=getContentPane();//获取容器
公共void init(){
con.setLayout(新的FlowLayout());//设置FlowLayout
con.add(新的JLabel());//JLabel容器
con.add(inputBox);//输入框容器
con.add(runButton);//运行按钮容器
con.add(passwordLabel);//密码标签容器
con.add(passwordLabel2);//密码标签容器
runButton.addActionListener(this);//查看是否单击了run
addActionListener(this);//查看是否使用了输入框
}
已执行的公共无效操作(操作事件e){
如果(!inputBox.getText().isEmpty()){
短语.更改字母(
inputBox.getText().charAt(0));//从用户获取输入
passwordLabel2.setText(phrase.hiddenprase);
}
}
}
公开课密语{
int错误=0;//暂时忽略
String phrase=“Bad hair day”//隐藏,用户必须猜测的内容
字符串HIDDENPHASE=“****”;//用户最初看到的内容
公共无效更改字母(字符输入){
StringBuilder检查器=新的StringBuilder(HiddenPhase);
布尔错误猜测=真;

对于(int i=0;您没有任何地方可以尝试更新
passwordLabel2
标签。您甚至没有调用
changeLetter()
每次我尝试调用changeLetter时,都会遇到错误。非常感谢!有一件事我真的很困惑。当我创建stringbuilder时,它给了我一个错误,除非我使用void stringbuilder方法,否则它不会消失。你为什么不需要它来让它工作?“stringbuilder(HiddenPhase);”行被识别为对此方法StringBuilder的调用。您的程序不需要此方法私有void StringBuilder(String HiddenPhase){//TODO自动生成的方法存根},因此也不需要调用StringBuilder(HiddenPhase)。
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++;
         }
    }
}