简体   繁体   English

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

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

I have tried a bunch of arrangements for my code, and this is my last version of code. 我已经尝试了一些代码安排,这是我的最新版本。 I am trying to create a window, that will cycle through my 5 questions, and the end it will display how many you got correct. 我正在尝试创建一个窗口,该窗口将循环浏览我的5个问题,最后将显示您有多少个正确答案。

Currently after entering my first answer in the text field it jumps to the end of the array. 目前,在文本字段中输入我的第一个答案后,它跳到数组的末尾。 After that it will continually stay on the same question. 之后,它将继续停留在同一问题上。

Sorry if I have made many coding errors, as this is my first time creating a program after watching a bunch of tutorials. 抱歉,我犯了很多编码错误,因为这是我看了很多教程后第一次创建程序。 Thanks for any help! 谢谢你的帮助!

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);
    }
}

During each actionPerformed call you cycle through the whole list. 在每个actionPerformed调用期间,您将循环浏览整个列表。 Here: 这里:

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

So by the time the text is actually displayed again, you have set to it to each different question, but stop with the last one and that is what the user actually sees. 因此,在再次实际显示文本时,您已经将其设置为每个不同的问题,但是从最后一个问题开始,这就是用户实际看到的内容。 You only want to change the text once, to the next question, everytime an action is performed. 每次执行操作时,您只想将文本更改为下一个问题。

You'll need to keep some sort of counter for which question you are on that can be accessed by the actionPerformed method 您需要保留某种计数器,您可以针对该问题使用actionPerformed方法访问

Also, as mentioned in the comments, you will want to change your result checking to use the equals method. 另外,如评论中所述,您将想要更改结果检查以使用equals方法。 Strings can't be compared using the == sign because for Strings == compares the reference each String object is pointing to, not the value of the String object 不能使用==符号来比较字符串,因为对于Strings ==比较每个String对象指向的引用,而不是String对象的值

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

The issue is the while() loop inside your ActionListener, it will always iterate through the entire set of questions and finish at the last entry. 问题是ActionListener中的while()循环,它将始终遍历整个问题集并在最后一个条目处完成。 This is due to the fact that your x and y variables are local to the scope of the ActionListener, and as such, are always reset to 0 and looped on each input click. 这是因为您的x和y变量在ActionListener的作用域内是局部的,因此,始终将其重置为0并在每次输入单击时循环。

To fix this, you don't need a while loop, just make your x variable a private class field (questionIndex), use an if statement ensure the index is within the array bounds, and update the problem and response values accordingly. 要解决此问题,您不需要while循环,只需将x变量设置为私有类字段(questionIndex),使用if语句确保索引在数组范围内,并相应地更新问题和响应值。

Here's some psuedocode that should do the right thing: 这是一些应该做正确的事情的伪代码:

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