0
私は20の質問があり、テキストファイルから配列として読み込まれるBEDMASテストを行いました。質問を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 ;
}
}
あなたは自分の配列をランダム化するために探していますか? http://stackoverflow.com/questions/1519736/random-shuffling-of-an-arrayこれを行う方法を示します。コレクションはこの機能を提供します。 – Kylar