gpt4 book ai didi

java - 试图制作一个刽子手程序,无法让它显示猜测的字母

转载 作者:行者123 更新时间:2023-11-30 04:08:31 25 4
gpt4 key购买 nike

我正在尝试创建一个刽子手程序。他们必须猜测的短语是“bad Hair day”,他们看到“* * **”。当用户输入字符时,没有任何变化。我不能 100% 确定我是否出错了,但可能是在 passwordlabel2 中或循环中的某个地方。

演示类(class)

 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

}

}
<小时/>

用户界面类

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





}
}

最佳答案

您必须制作 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++;
}
}
}

关于java - 试图制作一个刽子手程序,无法让它显示猜测的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com