简体   繁体   中英

Displaying random questions from a text file as an array

I have made a BEDMAS test, in which there are 20 questions and these are read from a text file as an array. I displayed the questions in a GUI, and you answer them in a input box underneath. I am stuck on randomizing the questions so that any question will show up, but I do not want the same question to show up at the same time. How do I do that.

My code is;

import javax.swing.* ;
import java.text.* ;
import java.io.* ;
import java.util.*;
import java.awt.event.* ;
import java.util.concurrent.ThreadLocalRandom ;
/**
 * Date: Jan 2017
 * Description: BEDMAS TEST made up of 20 questions that tests your order of operations skills.
 * Method List: 
 * double markCalculator (int input)
 */
public class TheBEDMASTest extends JFrame implements ActionListener {
            JLabel lblRead, lblPic, lblQuestion, lblBackGnd, lblOutcome ;
            JTextField txtQuestion, txtAnswer, txtOutcome ;
            JButton btnNext, btnClear ;

            static String username = "", mathQuestions [ ] ;
            static String mathAnswers [ ] ; 
            static int userAnswer = 0 ; 
            static int randomNumbers [ ] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19} ;
            static int j ; 
            static int m ;
            static double finalPercent ;

            NumberFormat percent = NumberFormat.getPercentInstance ( ) ;


            public TheBEDMASTest() { 
                        super ("BEDMAS TEST") ;

                        // Creates Labels
                        lblRead = new JLabel ("Your Answer") ;
                        lblQuestion = new JLabel ("The Question") ;
                        lblPic = new JLabel (new ImageIcon ("orderbedmas.png")) ;
                        lblOutcome = new JLabel ("The Outcome") ;

                        j =   m = 0 ;
                        // Creates the TextFields
                        txtQuestion = new JTextField (mathQuestions[j]) ;
                        txtAnswer = new JTextField ( ) ;
                        txtOutcome = new JTextField ( ) ;

                        // Create buttons
                        btnNext = new JButton ("Confirm") ;
                        btnClear = new JButton ("Clear") ;

                        // Set window layout
                        setLayout (null) ;
                        setSize(700, 300) ;

                        lblQuestion.setBounds(10, 15, 150, 15) ;
                        add (lblQuestion) ;

                        lblRead.setBounds(10, 50, 150, 15) ;
                        add (lblRead) ;

                        lblOutcome.setBounds(10, 85, 150, 15) ;
                        add (lblOutcome) ; 

                        txtQuestion.setBounds(160, 10, 150, 25) ;
                        add(txtQuestion) ;

                        txtAnswer.setBounds(160, 45, 150, 25) ;
                        add (txtAnswer) ;

                        txtOutcome.setBounds(160, 80, 150, 25) ;
                        add (txtOutcome) ;

                        btnNext.setBounds(70, 175, 120, 70) ;
                        add(btnNext) ;

                        btnClear.setBounds(200, 175, 120, 70) ;
                        add(btnClear) ;

                        lblPic.setBounds(375, 3, 283, 254) ;
                        add(lblPic) ;

                        btnNext.addActionListener (this) ;
                        btnClear.addActionListener (this) ;

                        // Sets the window to visible
                        setVisible(true) ; 
            }

            public void actionPerformed(ActionEvent evt) {
                        if (evt.getSource ( ) == btnNext) {
                                    String phraseOut ;
                                    String phraseIn ; 



                                    phraseIn = txtAnswer.getText( ) ;

                                    phraseOut = correctChecker(phraseIn) ;
                                    System.out.println(phraseOut) ;

                                    txtOutcome.setText(phraseOut) ;
                                    txtQuestion.setText(mathQuestions[j]) ;
                                    System.out.println(mathQuestions[j]) ;
                                    j++ ;
                                    txtQuestion.setText(mathQuestions[j]) ;
                                    txtAnswer.setText("") ;

                                    if (j == 20) {
                                                finalPercent = markCalculator(m) ;
                                    }

                        }

                        else if (evt.getSource ( ) == btnClear) {
                                    txtAnswer.setText("") ;
                                    txtOutcome.setText("") ;
                        }
            }

            public static String correctChecker (String mathA) {

                        if (mathA.equalsIgnoreCase(mathAnswers[j])) {
                                    m = m+ 1 ;
                                    System.out.println(m) ;
                                    return "Correct!" ;

                        }

                        else if (mathA.equalsIgnoreCase(mathAnswers[j]) == false) {
                                    return "Incorrect!" ;
                        }

                        return "Unknown" ;
            }


            public static void main(String[] args) throws IOException {
                        // Declaring variables for question arrays, answers arrays, random number arrays
//                        int k = 0 ;


                        // (needs work)
                        // Shuffle's randomNumbers array
                        shuffleArray(randomNumbers) ;


                        mathQuestions = new String [20] ;
                        mathAnswers = new String [20] ;

                        // Prompts user for name and reads question and array texts

                        // UNCOMMENT
//                        username = IO.readString("What is your name?") ;

                        FileReader fileQ = new FileReader ("mathQuestions.txt") ;
                        BufferedReader input = new BufferedReader (fileQ) ;

                        FileReader fileA = new FileReader ("questionsAnswers.txt") ;
                        BufferedReader input2 = new BufferedReader (fileA) ;

                        for (int i = 0 ; i < mathQuestions.length ; i++) {
                                    mathQuestions[i] = input.readLine( ) ;
                                    mathAnswers[i] = input2.readLine( ) ;

                        }

                        // Prompts user for answer and displays question, checks for correct answer or not
                        for (int j = 0 ; j < mathQuestions.length ; j++) {

                                    // (needs work)
//                                    k = IO.readInt("" + randomNumbers[j]) ;

                                    // UNCOMMENT
//                                    userAnswer = IO.readInt(mathQuestions[j]) ;
//                                    if (userAnswer == mathAnswers[j]) {
//                                                IO.display("Correct!") ;
//                                                m = m+ 1 ;
//                                    }
//                                    
//                                    else if (userAnswer != mathAnswers[j]) {
//                                                IO.display("Incorrect!") ;
//                                    }
                        }   

                        // Calls mark calculator to calculate and display percent mark
                        finalPercent = markCalculator(m) ;

//                        IO.display(username + "\n" + percent.format(finalPercent)) ;

                        new TheBEDMASTest ( ) ;

                        fileQ.close ( ) ;
                        fileA.close ( ) ;
            }

            // Shuffle method (needs work)
            static void shuffleArray(int[ ] ar)
            {
                        Random rnd = ThreadLocalRandom.current();
                        for (int i = ar.length - 1; i > 0; i--)
                        {
                                    int index = rnd.nextInt(i + 1);
                                    // Simple swap
                                    int a = ar[index];
                                    ar[index] = ar[i];
                                    ar[i] = a;
                        }
            }

            // Mark calculator method
            public static double markCalculator (int input) {
                        double userPercent = 0 ;
                        userPercent = (input / 20) ;
                        return userPercent ;
            }
}

You should have a collection of all indices that correspond to the questions, then when you put your test together all you do is call Collections.shuffle(). Then loop based on how many questions you want to ask the user.

Pseudo Code

ArrayList<Integer> indices = new ArrayList<>(# of all questions from file);
for(int index = 0 < index < # of all questions from file; index++)
    indices.add(index);

Collections.shuffle(indices);

for(int question = 0; question < # questions for test; question++)
    display the Question that corresponds to indices.get(question);

Example of shuffle

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