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

时间:2014-10-13 03:58:50

标签: java string

我正在尝试制作一款解读单词的游戏。但我似乎无法正确检查用户的猜测是否等于实际答案。似乎无论什么实际的单词都不等于用户的猜测,也就是你在这里。
有没有其他方法来暂停我的代码并等待用户输入而不是有一个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("");

    }
}

1 个答案:

答案 0 :(得分:1)

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

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