口算题卡
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
class Main extends JFrame {
private JTextArea questionArea;
private JTextField[] answerFields;
private JButton submitButton;
private JLabel resultLabel;
private int correctCount = 0;
private int wrongCount = 0;
private Random random = new Random();
private int remainingTime = 300; // 以秒为单位的剩余时间,修改为5分钟
private ArrayList
private Date startTime;
public Main() {
setTitle("数学小测验");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
questionArea = new JTextArea();
generateAllQuestions();
JScrollPane scrollPane = new JScrollPane(questionArea);
panel.add(scrollPane, BorderLayout.CENTER);
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new GridLayout(30, 2));
answerFields = new JTextField[30];
for (int i = 0; i < 30; i++) {
answerFields[i] = new JTextField();
answerPanel.add(new JLabel("第" + (i + 1) + "题答案:"));
answerPanel.add(answerFields[i]);
}
panel.add(answerPanel, BorderLayout.SOUTH);
submitButton = new JButton("提交答案");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
checkAnswers();
Date endTime = new Date();
long diff = endTime.getTime() - startTime.getTime();
long diffSeconds = diff / 1000;
long minutes = diffSeconds / 60;
long seconds = diffSeconds % 60;
JOptionPane.showMessageDialog(null, "你完成所有题目共用了 " + minutes + " 分钟 " + seconds + " 秒");
showFullResults();
}
});
resultLabel = new JLabel();
panel.add(submitButton, BorderLayout.EAST);
panel.add(resultLabel, BorderLayout.NORTH);
add(panel);
startTime = new Date();
}
private void generateAllQuestions() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 30; i++) {
int n1 = random.nextInt(100);
int n2 = random.nextInt(100);
int choose = random.nextInt(4);
String op;
int expectedResult;
switch (choose) {
case 0:
op = "+";
expectedResult = n1 + n2;
break;
case 1:
if (n1 < n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
op = "-";
expectedResult = n1 - n2;
break;
case 2:
// 确保乘法结果不超过三位数
do {
n1 = random.nextInt(100);
n2 = random.nextInt(100);
} while (n1 * n2 >= 1000);
op = "*";
expectedResult = n1 * n2;
break;
case 3:
n2 = getDivisor(n1, random);
op = "/";
expectedResult = n1 / n2;
break;
default:
op = "%";
expectedResult = n1 % n2;
break;
}
sb.append((i + 1)).append(". ").append(n1).append(" ").append(op).append(" ").append(n2).append(" = \n");
Question question = new Question(n1 + " " + op + " " + n2 + " =", 0, expectedResult);
questionList.add(question);
}
questionArea.setText(sb.toString());
}
private void checkAnswers() {
correctCount = 0;
wrongCount = 0;
for (int i = 0; i < 30; i++) {
int userAnswer;
try {
userAnswer = Integer.parseInt(answerFields[i].getText());
if (userAnswer == questionList.get(i).getExpectedAnswer()) {
correctCount++;
} else {
wrongCount++;
}
} catch (NumberFormatException ex) {
wrongCount++;
}
}
}
private int getDivisor(int n1, Random random) {
int divisor;
do {
divisor = random.nextInt(99)+1;
} while (n1 % divisor!= 0);
return divisor;
}
private void showFullResults() {
double totalQuestions = 30.0;
double correct = correctCount;
double accuracy = (correct * 100 / totalQuestions);
DecimalFormat df = new DecimalFormat("0.00");
StringBuilder resultMessage = new StringBuilder();
resultMessage.append("错题数:").append(wrongCount).append("\n");
resultMessage.append("正确率:").append(df.format(accuracy)).append("%\n");
resultMessage.append("题目\t\t用户答案\t正确答案\n");
for (Question question : questionList) {
int index = questionList.indexOf(question);
resultMessage.append(question.getQuestion()).append("\t\t");
resultMessage.append(answerFields[index].getText()).append("\t ");
resultMessage.append(question.getExpectedAnswer()).append("\n");
}
JOptionPane.showMessageDialog(null, resultMessage.toString());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main quiz = new Main();
quiz.setVisible(true);
}
});
}
}
class Question {
private String question;
private int userAnswer;
private int expectedAnswer;
public Question(String question, int userAnswer, int expectedAnswer) {
this.question = question;
this.userAnswer = userAnswer;
this.expectedAnswer = expectedAnswer;
}
public String getQuestion() {
return question;
}
public int getUserAnswer() {
return userAnswer;
}
public int getExpectedAnswer() {
return expectedAnswer;
}
}