简体   繁体   中英

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.

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. 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

Also, as mentioned in the comments, you will want to change your result checking to use the equals method. 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

 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. 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.

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.

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

        ...

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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