gpt4 book ai didi

java - 程序无法识别字符串是什么

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:26 25 4
gpt4 key购买 nike

我正在尝试制作一款可以解读单词的游戏。但我似乎无法让它正确检查用户的猜测是否等于实际答案。似乎无论什么实际单词都不等于用户的猜测,同时你也在这里。
除了 while 循环之外,还有其他方法可以暂停我的代码并等待用户输入吗?

import javax.swing.JOptionPane;

public class Loop extends CreateWindow{
private static final long serialVersionUID = 1L;
public static boolean running = true;

public static void GameStart(){
if(running){
int wordNum = 0;
for (int i=0;i<=9;i++){
field.setText("");
setScrambledWord(i);
while(enter == false){
System.out.println(userAnswer);
System.out.println(enter);
System.out.println(score);
}
userAnswer = area.getText();
if(userAnswer == normalWords[wordNum]){
JOptionPane.showMessageDialog(null, "legos", userAnswer, JOptionPane.OK_OPTION);
}else if(userAnswer != normalWords[wordNum]){
JOptionPane.showMessageDialog(null, "sogel", userAnswer, JOptionPane.OK_OPTION);
}
area.setText("");
enter = false;
wordNum++;
}
}
}

public static void main(String[] args){
new CreateWindow();
running = true;
GameStart();
}
}



import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class CreateWindow extends JFrame {
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
public static int score = 0;
static JTextArea area = new JTextArea(10,40);
static JTextField field = new JTextField(40);
JButton button = new JButton("Press to Enter Answer");
public static boolean enter;
public static String userAnswer;
public static String[] normalWords = {
"yellow",
"alphabet",
"tangent",
"elephant",
"amusement",
"cat",
"eerie",
"wonder",
"shadows"
};
public static String[] scrambledWords = {
"lloeyw",
"lphaabte",
"natgnte",
"leehpnta",
"mauseenmt",
"tca",
"reeei",
"dowenr",
"shawdos"
};
CreateWindow(){
setSize(500,300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

field.setEditable(false);

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
enter = true;
System.out.println("You clicked the button");
System.out.println(enter);
}
});
add(panel);
panel.add(field, BorderLayout.PAGE_START);
panel.add(area, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
setVisible(true);
}

public static void setScrambledWord(int i){
field.setText(scrambledWords[i]);
}

public void clearArea(){
area.setText("");

}
}

最佳答案

== 比较对象的标识,这意味着它检查您比较的两个引用是否指向同一内存地址。

.equals() 根据对象的值来比较对象。

关于java - 程序无法识别字符串是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332703/

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