为什么我的程序没有在数组中循环

时间:2014-01-29 01:35:46

标签: java arrays swing while-loop

我为我的代码尝试了一些安排,这是我最后一个版本的代码。我正在尝试创建一个窗口,它将遍历我的5个问题,最后它将显示你有多少正确。

目前在文本字段中输入我的第一个答案后,它会跳转到数组的末尾。在此之后,它将继续保持同样的问题。

很抱歉,如果我犯了很多编码错误,因为这是我第一次看完一堆教程后创建一个程序。谢谢你的帮助!

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class testwin extends JFrame {
    private JTextField input;
    private JLabel problem;
    private String[] questions = new String[5];
    private String[] answers = new String[5];
    private String[] response = new String[5];
    private int total = 5;
    private int result = 0;
    private String mark;

    public testwin(){
        super("Pop Quiz");
        setLayout(new FlowLayout());

        questions[0] = "Solve for x \t (x + 3)*2-10 = 4";
        questions[1] = "Factorize \t x^2 + 10x + 21";
        questions[2] = "Find the square root of 64";
        questions[3] = "Multiply 23 and 94";
        questions[4] = "Add 2145, 1452, 253,1414";
        answers[0] = "4";
        answers[1] = "(x + 3)(x + 7)";
        answers[2] = "8";
        answers[3] = "2162";
        answers[4] = "5264";

        problem = new JLabel(questions[0]);
        add(problem);

        input = new JTextField("Answer goes here",20);
        add(input);

        mark = String.format("You got %s correct out of %s", result,total);

        input.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    int y = 0;
                    int x = 0;
                    if(event.getSource() == input){ 
                        while(x < questions.length){
                            problem.setText(questions[x]);
                            response[y] = String.format("%s",event.getActionCommand());
                            input.setText("Answer goes here");
                            x++;
                            y++;
                        }
                    }
                    if(x == 4)
                        JOptionPane.showMessageDialog(null, mark);
                    for(int z = 0; z < questions.length; z++){
                        if(response[z] == answers[z])
                            result++;
                    }
                }
            }   
        );
        add(input);

        setSize(250,250);
        setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:0)

在每个actionPerformed调用期间,您将遍历整个列表。这里:

    while(x < questions.length){
        problem.setText(questions[x]);
        response[y] = String.format("%s",event.getActionCommand());
        input.setText("Answer goes here");
        x++;
        y++;
   }

因此,当文本再次实际显示时,您已将其设置为每个不同的问题,但停止使用最后一个问题,这是用户实际看到的内容。您只想在每次执行某个操作时将文本更改为下一个问题。

您需要保留某种计数器,以便您可以通过actionPerformed方法访问您所处的问题

另外,如评论中所述,您需要更改结果检查以使用equals方法。无法使用==符号比较字符串,因为字符串==会比较每个String对象所指向的引用,而不是String对象的值

 if(response[z].equals(answers[z]))
     result++;

答案 1 :(得分:0)

问题是ActionListener中的while()循环,它将始终遍历整个问题集并在最后一个条目处完成。这是因为你的x和y变量是ActionListener范围的本地变量,因此,总是重置为0并在每次输入点击时循环。

要解决此问题,您不需要while循环,只需将x变量设为私有类字段(questionIndex),使用if语句确保索引在数组范围内,并相应地更新问题和响应值

这是一些应该做正确事情的伪代码:

private int questionIndex = 0;

public void actionPerformed(ActionEvent event){
    if(event.getSource() == input){ 
        if(questionIndex < questions.length){
            problem.setText(questions[questionIndex]);
            response[questionIndex] = String.format("%s",event.getActionCommand());
            input.setText("Answer goes here");
            questionIndex++;
        }

        ...

    }
}