我正在尝试在java中构建一个多项选择测试,由于某种原因,我的代码仅适用于两个问题,而不再有效

这是我的测验的代码。


import java.util.*;

    import java.util.List;

    import javax.swing.*;

    import java.awt.*;

    import java.awt.event.*;

    public class Main2 {

        public class Questions {

            String Question;

            String userAns;

            String realAns;

        }

        public static void main(String[] args) {

            JFrame frame = new JFrame ("Screen");

            frame.setSize(2500, 2500);

            frame.setLayout(null);

            frame.setVisible(true);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            JPanel panel = new JPanel();

            frame.getContentPane().add(panel);

            panel.setSize(2500, 2500);

            panel.setLayout(null);  

            //panel.setBackground(Color.red);


            Question[] questions = new Question[2];

            System.out.println(questions.length);

            Question q1 = new Question();

            q1.Question = "1) What is your Name?";

            q1.A ="Bob";

            q1.B="Billy";

            q1.C="Joe";

            q1.D="Jill";

            questions[0] = q1;


            Question q2 = new Question();

            q2.Question = "2) What is your Age?";

            q2.A ="5";

            q2.B="69";

            q2.C="21";

            q2.D="12";

            questions[1] = q2;


            /*

            Question q3 = new Question();

            q3.Question = "3) When Is your Birthday?";

            q3.A = "May";

            q3.B="Jan";

            q3.C="Apr";

            q3.D="Aug";

            questions[2] = q3;


我的代码只用两个问题就可以正常工作。当我在其中添加第三个时,它甚至没有显示问题的文本。我尝试调试它以查看我的代码是否仍在运行。似乎它确实运行了。我工作了几个小时,但我徒劳无功,迫切需要帮助。


侃侃无极
浏览 119回答 2
2回答

慕尼黑5688855

public class Main2 {&nbsp; &nbsp; public class Questions {&nbsp; &nbsp; &nbsp; &nbsp; String Question;&nbsp; &nbsp; &nbsp; &nbsp; String userAns;&nbsp; &nbsp; &nbsp; &nbsp; String realAns;&nbsp; &nbsp; }&nbsp; &nbsp; static int count = 0;&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Screen");&nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(2500, 2500);&nbsp; &nbsp; &nbsp; &nbsp; frame.setLayout(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; JPanel panel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; frame.getContentPane().add(panel);&nbsp; &nbsp; &nbsp; &nbsp; panel.setSize(2500, 2500);&nbsp; &nbsp; &nbsp; &nbsp; panel.setLayout(null);&nbsp; &nbsp; &nbsp; &nbsp; //panel.setBackground(Color.red);&nbsp; &nbsp; &nbsp; &nbsp; Question[] questions = new Question[3];&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(questions.length);&nbsp; &nbsp; &nbsp; &nbsp; Question q1 = new Question();&nbsp; &nbsp; &nbsp; &nbsp; q1.Question = "1) What is your Name?";&nbsp; &nbsp; &nbsp; &nbsp; q1.A = "Bob";&nbsp; &nbsp; &nbsp; &nbsp; q1.B = "Billy";&nbsp; &nbsp; &nbsp; &nbsp; q1.C = "Joe";&nbsp; &nbsp; &nbsp; &nbsp; q1.D = "Jill";&nbsp; &nbsp; &nbsp; &nbsp; questions[0] = q1;&nbsp; &nbsp; &nbsp; &nbsp; Question q2 = new Question();&nbsp; &nbsp; &nbsp; &nbsp; q2.Question = "2) What is your Age?";&nbsp; &nbsp; &nbsp; &nbsp; q2.A = "5";&nbsp; &nbsp; &nbsp; &nbsp; q2.B = "69";&nbsp; &nbsp; &nbsp; &nbsp; q2.C = "21";&nbsp; &nbsp; &nbsp; &nbsp; q2.D = "12";&nbsp; &nbsp; &nbsp; &nbsp; questions[1] = q2;&nbsp; &nbsp; &nbsp; &nbsp; Question q3 = new Question();&nbsp; &nbsp; &nbsp; &nbsp; q3.Question = "3) When Is your Birthday?";&nbsp; &nbsp; &nbsp; &nbsp; q3.A = "May";&nbsp; &nbsp; &nbsp; &nbsp; q3.B = "Jan";&nbsp; &nbsp; &nbsp; &nbsp; q3.C = "Apr";&nbsp; &nbsp; &nbsp; &nbsp; q3.D = "Aug";&nbsp; &nbsp; &nbsp; &nbsp; questions[2] = q3;&nbsp; &nbsp; &nbsp; &nbsp; //When this question is added the code breaks down&nbsp; &nbsp; &nbsp; &nbsp; JLabel Question = new JLabel(questions[0].Question);&nbsp; &nbsp; &nbsp; &nbsp; Question.setBounds(50, 0, 1500, 50);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(Question);&nbsp; &nbsp; &nbsp; &nbsp; Question.setFont(new Font(Question.getFont().getName(), Font.PLAIN, 25));&nbsp; &nbsp; &nbsp; &nbsp; Question.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; JLabel incorrectAnswerLabel = new JLabel("Incorrect Answer! Try Again");&nbsp; &nbsp; &nbsp; &nbsp; incorrectAnswerLabel.setBounds(300, 0, 1000, 500);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(incorrectAnswerLabel);&nbsp; &nbsp; &nbsp; &nbsp; incorrectAnswerLabel.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; incorrectAnswerLabel.setFont(new Font(incorrectAnswerLabel.getFont().getName(), Font.BOLD, 46));&nbsp; &nbsp; &nbsp; &nbsp; incorrectAnswerLabel.setForeground(Color.RED);&nbsp; &nbsp; &nbsp; &nbsp; JLabel correctAnswerLabel = new JLabel("Correct Answer! Good Job");&nbsp; &nbsp; &nbsp; &nbsp; correctAnswerLabel.setBounds(300, 0, 1000, 500);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(correctAnswerLabel);&nbsp; &nbsp; &nbsp; &nbsp; correctAnswerLabel.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; correctAnswerLabel.setFont(new Font(correctAnswerLabel.getFont().getName(), Font.BOLD, 46));&nbsp; &nbsp; &nbsp; &nbsp; correctAnswerLabel.setForeground(Color.GREEN);&nbsp; &nbsp; &nbsp; &nbsp; JButton submitButton = new JButton();&nbsp; &nbsp; &nbsp; &nbsp; submitButton.setBounds(50, 250, 150, 50);&nbsp; &nbsp; &nbsp; &nbsp; submitButton.setText("Submit");&nbsp; &nbsp; &nbsp; &nbsp; panel.add(submitButton);&nbsp; &nbsp; &nbsp; &nbsp; submitButton.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; JRadioButton OptionA = new JRadioButton(questions[0].A);&nbsp; &nbsp; &nbsp; &nbsp; OptionA.setBounds(50, 50, 100, 50);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(OptionA);&nbsp; &nbsp; &nbsp; &nbsp; OptionA.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; JRadioButton OptionB = new JRadioButton(questions[0].B);&nbsp; &nbsp; &nbsp; &nbsp; OptionB.setBounds(50, 100, 100, 50);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(OptionB);&nbsp; &nbsp; &nbsp; &nbsp; OptionB.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; JRadioButton OptionC = new JRadioButton(questions[0].C);&nbsp; &nbsp; &nbsp; &nbsp; OptionC.setBounds(50, 150, 100, 50);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(OptionC);&nbsp; &nbsp; &nbsp; &nbsp; OptionC.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; JRadioButton OptionD = new JRadioButton(questions[0].D);&nbsp; &nbsp; &nbsp; &nbsp; OptionD.setBounds(50, 200, 100, 50);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(OptionD);&nbsp; &nbsp; &nbsp; &nbsp; OptionD.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; ButtonGroup radioGroup = new ButtonGroup();&nbsp; &nbsp; &nbsp; &nbsp; radioGroup.add(OptionA);&nbsp; &nbsp; &nbsp; &nbsp; radioGroup.add(OptionB);&nbsp; &nbsp; &nbsp; &nbsp; radioGroup.add(OptionC);&nbsp; &nbsp; &nbsp; &nbsp; radioGroup.add(OptionD);&nbsp; &nbsp; &nbsp; &nbsp; submitButton.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (submitButton.getText().equals("Submit")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (OptionA.isSelected()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submitButton.setText("Next Question");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correctAnswerLabel.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; incorrectAnswerLabel.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correctAnswerLabel.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submitButton.setText("Submit");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count < questions.length-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = count+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Question q = questions[count];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Question.setText(q.Question);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAnswers(q.A, q.B, q.C, q.D, OptionA, OptionB, OptionC, OptionD);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; static void setAnswers(String A, String B, String C, String D, JRadioButton a, JRadioButton b, JRadioButton c, JRadioButton d) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> answers = Arrays.asList(A, B, C, D);&nbsp; &nbsp; &nbsp; &nbsp; Collections.shuffle(answers);&nbsp; &nbsp; &nbsp; &nbsp; a.setText(answers.get(0));&nbsp; &nbsp; &nbsp; &nbsp; b.setText(answers.get(1));&nbsp; &nbsp; &nbsp; &nbsp; c.setText(answers.get(2));&nbsp; &nbsp; &nbsp; &nbsp; d.setText(answers.get(3));&nbsp; &nbsp; }}

ABOUTYOU

数组索引从零开始,但长度从 1 开始。如果您想添加第三个问题,则以下方法将起作用。问题[]问题=新问题[3];数组不是动态的,因此如果要动态添加问题,请使用 ArrayList。否则,将数组的长度增加到您觉得合适的大小。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java