Java 将文本文件中的随机问题显示为数组
我做了一个BEDMAS测试,其中有20个问题,这些问题是从一个文本文件作为数组读取的。我在GUI中显示了这些问题,您可以在下面的输入框中回答它们。我被困在随机化的问题,以便任何问题都会出现,但我不希望同一个问题出现在同一时间。我该怎么做呢 我的代码是Java 将文本文件中的随机问题显示为数组,java,Java,我做了一个BEDMAS测试,其中有20个问题,这些问题是从一个文本文件作为数组读取的。我在GUI中显示了这些问题,您可以在下面的输入框中回答它们。我被困在随机化的问题,以便任何问题都会出现,但我不希望同一个问题出现在同一时间。我该怎么做呢 我的代码是 import javax.swing.* ; import java.text.* ; import java.io.* ; import java.util.*; import java.awt.event.* ; import java.uti
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 ;
}
}
import javax.swing.*;
导入java.text.*;
导入java.io.*;
导入java.util.*;
导入java.awt.event.*;
导入java.util.concurrent.ThreadLocalRandom;
/**
*日期:2017年1月
*描述:BEDMAS测试由20个问题组成,测试您的操作技能顺序。
*方法列表:
*双标记计算器(整数输入)
*/
公共类thebedmatest扩展了JFrame,实现了ActionListener{
JLabel lblRead、lblPic、lblQuestion、lblBackGnd、lbloutcom;
JTextField txtQuestion、txtAnswer、txtOutput;
JButton btnNext,btnClear;
静态字符串username=“”,mathQuestions[];
静态字符串mathAnswers[];
静态int userAnswer=0;
静态整数随机数[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
静态int j;
静态int-m;
静态双最终百分比;
NumberFormat percent=NumberFormat.getPercentInstance();
公共床上测试(){
超级(“贝德马斯测试”);
//创建标签
lblRead=newjlabel(“您的答案”);
lblQuestion=新的JLabel(“问题”);
lblPic=newjlabel(newimageicon(“orderbedmas.png”);
lbloutcom=新的JLabel(“结果”);
j=m=0;
//创建文本字段
txtQuestion=新的JTextField(mathQuestions[j]);
txtAnswer=newjtextfield();
txtOutput=newjtextfield();
//创建按钮
btnNext=新按钮(“确认”);
btnClear=新的JButton(“清除”);
//设置窗口布局
setLayout(空);
设置大小(700300);
LBL问题。挫折(10,15,150,15);
添加(LBL问题);
lblhead.立根(10,50,150,15);
添加(lblRead);
lblOutcome.backbounds(10,85,150,15);
添加(lblOutcome);
问题.挫折(160,10,150,25);
添加(TXT问题);
txtAnswer.setBounds(160,45,150,25);
添加(txtAnswer);
TXTOUTION.setBounds(160,80,150,25);
添加(TXT结果);
b下一个立根(70,175,120,70);
添加(btnNext);
b包括立根(200、175、120、70);
添加(btnClear);
lblPic.立根(375,3283254);
添加(lblPic);
btnNext.addActionListener(此);
btnClear.addActionListener(此);
//将窗口设置为可见
setVisible(真);
}
已执行的公共无效操作(操作事件evt){
if(evt.getSource()==btnNext){
串出短语;
字符串短语;
phraseIn=txtAnswer.getText();
phraseOut=correctChecker(phraseIn);
System.out.println(短语输出);
setText(短语输出);
setText(mathQuestions[j]);
System.out.println(mathQuestions[j]);
j++;
setText(mathQuestions[j]);
txtAnswer.setText(“”);
如果(j==20){
最终百分比=马克计算器(m);
}
}
else if(evt.getSource()==btnClear){
txtAnswer.setText(“”);
txtOutput.setText(“”);
}
}
公共静态字符串校正器(字符串数学){
if(mathA.equalsIgnoreCase(mathAnswers[j])){
m=m+1;
系统输出打印项次(m);
返回“正确!”;
}
else if(mathA.equalsIgnoreCase(mathAnswers[j])==false){
返回“不正确!”;
}
返回“未知”;
}
公共静态void main(字符串[]args)引发IOException{
//为问题数组、答案数组、随机数数组声明变量
//int k=0;
//(需要工作)
//随机数数组
随机数;
mathQuestions=新字符串[20];
mathAnswers=新字符串[20];
//提示用户输入名称并读取问题和数组文本
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);