我做了一个BEDMAS测试,其中有20个问题,这些问题从文本文件中读取为数组。我在GUI中显示问题,然后在下面的输入框中回答它们。我坚持随机化问题,以便任何问题都会出现,但我不希望同一个问题出现在同一时间。我该怎么做。
我的代码是;
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 ;
}
}
答案 0 :(得分:0)
您应该拥有与问题相对应的所有索引的集合,然后当您将测试放在一起时,您只需调用Collections.shuffle()。然后根据您想要询问用户的问题进行循环。
伪代码
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);