import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class ArithmeticTestGUI extends JFrame {
private static final int TIME_LIMIT_SECONDS = 360;
private static final int QUESTION_COUNT = 30;
private JTextField[] questionFields;
private JTextField[] answerFields;
private JLabel timerLabel;
private JLabel resultLabel;
private JButton submitButton;
private int currentQuestionIndex;
private int correctCount;
private int[] questions;
private int[] answers;
private Timer timer;
private int timeLeft;
public ArithmeticTestGUI() {
setTitle("四则运算课堂测试");
setLayout(new BorderLayout());
initComponents();
startTest();
}
private void initComponents() {
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(5, 6));
questionFields = new JTextField[QUESTION_COUNT];
answerFields = new JTextField[QUESTION_COUNT];
for (int i = 0; i < QUESTION_COUNT; i++) {
JPanel questionAnswerPanel = new JPanel(new BorderLayout());
questionFields[i] = new JTextField(10);
questionFields[i].setEditable(false);
questionAnswerPanel.add(questionFields[i], BorderLayout.CENTER);
JPanel answerPanel = new JPanel(new BorderLayout());
answerFields[i] = new JTextField(5);
answerPanel.add(new JLabel("答案: "), BorderLayout.WEST);
answerPanel.add(answerFields[i], BorderLayout.CENTER);
questionAnswerPanel.add(answerPanel, BorderLayout.EAST);
topPanel.add(questionAnswerPanel);
}
JPanel bottomPanel = new JPanel();
timerLabel = new JLabel("时间: " + TIME_LIMIT_SECONDS + " 秒");
timerLabel.setFont(timerLabel.getFont().deriveFont(20f));
bottomPanel.add(timerLabel);
resultLabel = new JLabel();
bottomPanel.add(resultLabel);
submitButton = new JButton("提交");
submitButton.setFont(submitButton.getFont().deriveFont(20f));
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
checkAnswers();
}
});
bottomPanel.add(submitButton);
add(topPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) (screenSize.width * 0.8);
int height = (int) (screenSize.height * 0.8);
setSize(width, height);
}
private void startTest() {
currentQuestionIndex = 0;
correctCount = 0;
questions = new int[QUESTION_COUNT * 3];
answers = new int[QUESTION_COUNT];
Random random = new Random();
for (int i = 0; i < QUESTION_COUNT; i++) {
int[] question = generateQuestion(random);
questions[i * 3] = question[0];
questions[i * 3 + 1] = question[1];
questions[i * 3 + 2] = question[2];
}
timeLeft = TIME_LIMIT_SECONDS;
updateTimerLabel();
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (timeLeft > 0) {
timeLeft--;
updateTimerLabel();
} else {
timer.stop();
endTest();
}
}
});
timer.start();
showNextQuestions();
}
private void updateTimerLabel() {
timerLabel.setText("时间: " + timeLeft + " 秒");
}
private void showNextQuestions() {
if (currentQuestionIndex < QUESTION_COUNT) {
for (int i = 0; i < QUESTION_COUNT; i++) {
if (i < currentQuestionIndex) {
questionFields[i].setText("已回答");
answerFields[i].setEditable(false);
} else {
int a = questions[i * 3];
int b = questions[i * 3 + 1];
int operator = questions[i * 3 + 2];
String operatorSymbol = getOperatorSymbol(operator);
questionFields[i].setText(a + " " + operatorSymbol + " " + b + " = ");
answerFields[i].setText("");
answerFields[i].requestFocus();
}
}
} else {
endTest();
}
}
private void checkAnswers() {
for (int i = currentQuestionIndex; i < QUESTION_COUNT; i++) {
try {
int userAnswer = Integer.parseInt(answerFields[i].getText());
if (checkAnswer(questions[i * 3], questions[i * 3 + 1], questions[i * 3 + 2], userAnswer)) {
correctCount++;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "请输入有效的数字答案。");
return;
}
}
currentQuestionIndex = QUESTION_COUNT;
endTest();
}
private void endTest() {
timer.stop();
double correctRate = (double) correctCount / QUESTION_COUNT * 100;
resultLabel.setText("测试结束! 正确题数: " + correctCount + " 错误题数: " + (QUESTION_COUNT - correctCount) + " 正确率: " + correctRate + "%");
submitButton.setEnabled(false);
for (JTextField answerField : answerFields) {
answerField.setEditable(false);
}
}
private static int[] generateQuestion(Random random) {
int a, b, operator;
do {
a = random.nextInt(89) + 10;
b = random.nextInt(89) + 10;
operator = random.nextInt(4);
} while (!isValidQuestion(a, b, operator));
return new int[]{a, b, operator};
}
private static boolean isValidQuestion(int a, int b, int operator) {
switch (operator) {
case 0:
return true;
case 1:
return a >= b;
case 2:
return a * b < 1000;
case 3:
return b!= 0 && a % b == 0;
default:
return false;
}
}
private static String getOperatorSymbol(int operator) {
switch (operator) {
case 0:
return "+";
case 1:
return "-";
case 2:
return "*";
case 3:
return "/";
default:
return "";
}
}
private static boolean checkAnswer(int a, int b, int operator, int answer) {
int correctAnswer;
switch (operator) {
case 0:
correctAnswer = a + b;
break;
case 1:
correctAnswer = a - b;
break;
case 2:
correctAnswer = a * b;
break;
case 3:
correctAnswer = a / b;
break;
default:
return false;
}
return answer == correctAnswer;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ArithmeticTestGUI().setVisible(true));
}
}
标签:COUNT,return,int,四则运算,QUESTION,private,随机,三十,new From: https://www.cnblogs.com/bawangji666/p/18442432