繁体   English   中英

为什么我的程序不循环遍历数组

[英]Why is my program not cycling through the array

我已经尝试了一些代码安排,这是我的最新版本。 我正在尝试创建一个窗口,该窗口将循环浏览我的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);
    }
}

在每个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方法。 不能使用==符号来比较字符串,因为对于Strings ==比较每个String对象指向的引用,而不是String对象的值

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

问题是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++;
        }

        ...

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM