关于出四则运算题的进阶可视化解答
`import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
class QuizFrame extends JFrame {
private HashSet
private int correctAnswers;
private int totalProblems;
private int wrongAnswers;
private Random random;
private long startTime;
private long endTime;
private int countdownTime;
private JTextField[][] answerFields;
private JLabel timeLabel;
private JButton submitButton;
public QuizFrame() {
setTitle("四则运算测试");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
random = new Random();
generatedProblems = new HashSet<>();
correctAnswers = 0;
totalProblems = 30;
wrongAnswers = 0;
System.out.println("请输入做题时间");
Scanner scanner = new Scanner(System.in);
countdownTime = scanner.nextInt();
if (countdownTime > 300 || countdownTime <= 0) {
countdownTime = 60; // 默认一分钟
System.out.println("输入时间不合法,采用默认一分钟倒计时。");
}
startTime = System.currentTimeMillis();
endTime = startTime + countdownTime * 1000;
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel problemPanel = new JPanel(new GridLayout(6, 5));
problemPanel.setBorder(new TitledBorder("题目与答案"));
answerFields = new JTextField[6][5];
generateProblems();
int index = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (index < totalProblems) {
JLabel problemLabel = new JLabel(generatedProblems.toArray()[index].toString() + " = ");
problemPanel.add(problemLabel);
JTextField answerField = new JTextField();
answerFields[i][j] = answerField;
problemPanel.add(answerField);
index++;
}
}
}
mainPanel.add(problemPanel, BorderLayout.CENTER);
timeLabel = new JLabel();
mainPanel.add(timeLabel, BorderLayout.NORTH);
submitButton = new JButton("提交");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
checkAnswers();
}
});
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(submitButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
add(mainPanel);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
long remainingTime = (endTime - System.currentTimeMillis()) / 1000;
if (remainingTime >= 0) {
timeLabel.setText("剩余时间:" + remainingTime + " 秒");
} else {
timeLabel.setText("时间到!");
checkAnswers();
}
}
});
timer.start();
}
private void generateProblems() {
while (generatedProblems.size() < totalProblems) {
int num1 = random.nextInt(100);
int num2;
int operatorIndex = random.nextInt(4);
int result;
String operator;
String problem;
switch (operatorIndex) {
case 0:
operator = "+";
num2 = random.nextInt(100 - num1);
result = num1 + num2;
problem = num1 + " " + operator + " " + num2;
break;
case 1:
operator = "-";
num2 = random.nextInt(num1 + 1);
result = num1 - num2;
problem = num1 + " " + operator + " " + num2;
break;
case 2:
operator = "*";
do {
num2 = random.nextInt(100);
} while (num1 * num2 >= 1000);
result = num1 * num2;
problem = num1 + " " + operator + " " + num2;
break;
default:
operator = "/";
num2 = random.nextInt(100) + 1;
while (num1 % num2!= 0) {
num2 = random.nextInt(100) + 1;
}
result = num1 / num2;
problem = num1 + " " + operator + " " + num2;
break;
}
if (!generatedProblems.contains(problem)) {
generatedProblems.add(problem);
}
}
}
private void checkAnswers() {
if (correctAnswers == 0 && wrongAnswers == 0) {
int index = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (index < totalProblems) {
String problem = generatedProblems.toArray()[index].toString();
int expectedAnswer = calculateAnswer(problem);
try {
int userAnswer = Integer.parseInt(answerFields[i][j].getText());
if (userAnswer == expectedAnswer) {
correctAnswers++;
} else {
wrongAnswers++;
}
} catch (NumberFormatException ex) {
wrongAnswers++;
}
index++;
}
}
}
double accuracy = (double) correctAnswers / totalProblems * 100;
int option = JOptionPane.showConfirmDialog(this, "答题结束。\n错题数:" + wrongAnswers + "\n正确率:" + accuracy + "%", "结果", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
dispose();
}
}
}
private int calculateAnswer(String problem) {
String[] parts = problem.split(" ");
int num1 = Integer.parseInt(parts[0]);
int num2 = Integer.parseInt(parts[2]);
String operator = parts[1];
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
default:
return num1 / num2;
}
}
}
public class sice {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
QuizFrame frame = new QuizFrame();
frame.pack();
frame.setVisible(true);
});
}
}`