Java 循环字母输入
我正在为学校做一个刽子手游戏,我遇到了一个我根本无法解决的问题,也许我想得太多了,也许我没有。不管怎样,我需要让用户输入一个字母,如果这个字母是游戏中使用的单词(pikachu。我知道,这是一个愚蠢的选择,但它非常简单,所以我使用了它),那么这个字母就会暴露出来,问题是在输入一个字母后,用户无法猜出更多的字母。我需要一种方法来循环字母输入和显示,这样我就可以真正玩这个游戏了 如果解决方案如此简单,我很抱歉,但我就是不知道我的代码中需要修改什么才能解决问题,因为我对java非常陌生Java 循环字母输入,java,swing,loops,event-driven,Java,Swing,Loops,Event Driven,我正在为学校做一个刽子手游戏,我遇到了一个我根本无法解决的问题,也许我想得太多了,也许我没有。不管怎样,我需要让用户输入一个字母,如果这个字母是游戏中使用的单词(pikachu。我知道,这是一个愚蠢的选择,但它非常简单,所以我使用了它),那么这个字母就会暴露出来,问题是在输入一个字母后,用户无法猜出更多的字母。我需要一种方法来循环字母输入和显示,这样我就可以真正玩这个游戏了 如果解决方案如此简单,我很抱歉,但我就是不知道我的代码中需要修改什么才能解决问题,因为我对java非常陌生 import
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.KeyListener;
import javax.swing.*;
public class PanDisp extends JPanel {
JLabel lblOutput;
JLabel lblGuess;
JButton btnUpdateLabel;
Image imgPkmn;
FraImg fraImg;
String sSecret;
public PanDisp() {//Constructor
KeyInput keyInput = new KeyInput();
KeyInput.LabelChangeListener labelChange = keyInput.new LabelChangeListener();
sSecret = "*******";
lblGuess = new JLabel("Type will go here");
lblOutput = new JLabel(sSecret);
btnUpdateLabel = new JButton("Enter");
add(lblOutput);
add(btnUpdateLabel);
addKeyListener(new KeyInput());
setFocusable(true);
btnUpdateLabel.addActionListener(labelChange);
fraImg = new FraImg(imgPkmn);
}
public void GameOver() {
}
class KeyInput implements KeyListener {
String sInput;
String sWord = "pikachu";
String sSecret = "*******";
char chInput;
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
chInput = (char) e.getKeyChar();
sInput = String.valueOf(chInput);
lblOutput.setText(sInput);
}
@Override
public void keyReleased(KeyEvent e) {
}
class LabelChangeListener implements ActionListener {
char cWord;
int nCorrect, nIncorrect, nNum;
public void actionPerformed(ActionEvent event) {
if (sWord.contains(sInput)) {
for (int i = 0; i < sWord.length(); i++) {
sSecret.replace(sSecret.charAt(i), sWord.charAt(i));
}
nCorrect += 1;
}
else {
nIncorrect += 1;
if (nIncorrect == 7) {
GameOver();
}
}
}
}
}
}
import javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.KeyAdapter;
导入java.awt.event.KeyEvent;
导入java.awt.*;
导入java.awt.event.KeyListener;
导入javax.swing.*;
公共类PanDisp扩展了JPanel{
JLabel-lblOutput;
JLabel-lblGuess;
JButton btnUpdateLabel;
图像imgPkmn;
FraImg FraImg;
弦乐;
公共PanDisp(){//构造函数
KeyInput KeyInput=新的KeyInput();
KeyInput.LabelChangeListener labelChange=KeyInput.new LabelChangeListener();
sSecret=“*******”;
lblGuess=newjlabel(“类型将在此处显示”);
lblOutput=新的JLabel(sSecret);
btnUpdateLabel=新的JButton(“输入”);
添加(lblOutput);
添加(btnUpdateLabel);
addKeyListener(新的KeyInput());
设置聚焦(真);
btnUpdateLabel.addActionListener(labelChange);
fraImg=新fraImg(imgPkmn);
}
公众假期{
}
类KeyInput实现KeyListener{
弦输入;
弦剑=“皮卡丘”;
字符串sSecret=“*******”;
煤焦产量;
@凌驾
public void keyTyped(KeyEvent e){
}
@凌驾
按下公共无效键(按键事件e){
chInput=(char)e.getKeyChar();
sInput=String.valueOf(chInput);
lblOutput.setText(sInput);
}
@凌驾
公共无效密钥已释放(密钥事件e){
}
类LabelChangeListener实现ActionListener{
char-cWord;
int nCorrect,nIncorrect,nNum;
已执行的公共无效操作(操作事件){
如果(剑包含(辛普特)){
对于(int i=0;i
你的问题是你的心态不好,必须改变。不要想“循环”,事实上,要从方程中去掉“循环”。您正在一个事件驱动的编程环境中编程,您所考虑的循环属于线性控制台编程环境。相反,想想“对象的状态”和“行为改变到状态改变”,你会在这个探索中走得更远。因此,更改类的状态——猜测次数、正确猜测次数,然后根据此状态更改对用户输入的响应
例如,如果您想创建一个控制台程序,允许用户输入5个字符串,然后将这些字符串显示给用户,这将非常简单,因为您需要创建字符串数组,然后使用For循环提示用户输入文本5次,在循环中抓取每个输入的字符串。这里的“循环”与您请求的工作类似
线性控制台程序
import java.util.Scanner;
公共类企业5号1{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“请输入5个句子:”);
字符串[]句=新字符串[5];
for(int i=0;i<句子长度;i++){
System.out.printf(“输入句子#%d:,(i+1));
句子[i]=scanner.nextLine();
}
System.out.println(“您输入了以下句子:”);
for(字符串句子:句子){
System.out.println(句子);
}
scanner.close();
}
}
另一方面,如果您希望创建一个GUI,它执行类似的操作,提示用户输入5个字符串并将这些字符串接受到一个数组中,那么您不能使用相同类型的for循环。相反,您需要为类提供一个int字符串计数器,可能称为enteredSentenceCount,并且在JButton的ActionListener(或Action——非常类似)中,只有当enteredSentenceCount小于5时,才接受输入的字符串(可能输入到名为entryField的JTextField中),小于允许的最大字符串数。当然,每次输入字符串时,您都会递增enteredSentenceCount变量。这种增加一个计数器变量并检查其值的组合需要取代“循环”的概念。在这里,类的“状态”保存在enteredSentenceCount中,我们想要的行为改变是根据enteredSentenceCount的值改变按钮的动作——如果小于5,则接受字符串,如果等于或大于5,则显示输入的字符串 事件驱动GUI程序
导入java.awt.FlowLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
公共类Enter5Numbers2扩展JPanel{
私有静态final int MAX_station_COUNT=5;//要输入的字符串数
私有静态最终字符串提示符\u TEMPLATE=“请
import java.util.Scanner;
public class Enter5Numbers1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter 5 sentences:");
String[] sentences = new String[5];
for (int i = 0; i < sentences.length; i++) {
System.out.printf("Enter sentence #%d: ", (i + 1));
sentences[i] = scanner.nextLine();
}
System.out.println("You entered the following sentences:");
for (String sentence : sentences) {
System.out.println(sentence);
}
scanner.close();
}
}
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Enter5Numbers2 extends JPanel {
private static final int MAX_SENTENCE_COUNT = 5; // number of Strings to enter
private static final String PROMPT_TEMPLATE = "Please enter sentence number %d:";
private String[] sentences = new String[MAX_SENTENCE_COUNT]; // array to hold entered Strings
private int enteredSentenceCount = 0; // count of number of Strings entered
private JTextField entryField = new JTextField(20); // field to accept text input frm user.
// JLabel to display prompts to user:
private JLabel promptLabel = new JLabel(String.format(PROMPT_TEMPLATE, (enteredSentenceCount + 1)));
public Enter5Numbers2() {
// create GUI
// First create Action / ActionListener for button
EntryAction entryAction = new EntryAction("Enter");
JButton entryButton = new JButton(entryAction); // pass it into the button
entryField.setAction(entryAction); // but give it also to JTextField so that the enter key will trigger it
// JPanel to accept user data entry
JPanel entryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
entryPanel.add(entryField);
entryPanel.add(entryButton);
// allow main JPanel to display prompt
setBorder(BorderFactory.createTitledBorder("Please Enter 5 Sentences"));
setLayout(new GridLayout(2, 1));
add(promptLabel);
add(entryPanel);
}
// Action class, similar to an ActionListener
private class EntryAction extends AbstractAction {
public EntryAction(String name) {
super(name);
putValue(MNEMONIC_KEY, (int) name.charAt(0));
}
@Override
public void actionPerformed(ActionEvent e) {
// check that we haven't entered more than the max number of sentences
if (enteredSentenceCount < MAX_SENTENCE_COUNT) {
// if OK, get the entered text
String sentence = entryField.getText();
// put it in our array
sentences[enteredSentenceCount] = sentence;
entryField.setText(""); // clear the text field
entryField.requestFocusInWindow(); // set the cursor back into the textfield
enteredSentenceCount++; // increment our entered sentence count variable
promptLabel.setText(String.format(PROMPT_TEMPLATE, (enteredSentenceCount + 1))); // change prompt
}
// if the number of sentences added equals the number we want, display it
if (enteredSentenceCount == MAX_SENTENCE_COUNT) {
JTextArea textArea = new JTextArea(6, 30);
for (String sentence : sentences) {
textArea.append(sentence + "\n");
}
JScrollPane scrollPane = new JScrollPane(textArea);
JOptionPane.showMessageDialog(Enter5Numbers2.this, scrollPane, "Five Sentences Entered",
JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Enter 5 Numbers");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new Enter5Numbers2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}