2024/9/30日工作日志

完成Java课堂测试;
import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Question extends JFrame {
private JPasswordField[] password = new JPasswordField[30];
private int[] answer = new int[30];
private Random rand = new Random();
private double rightnum = 0;
private JLabel timerLabel;
private int timeLeft = 60;
private Timer timer;

public Question() {
    setTitle("四则运算");
    setSize(600, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setLayout(new GridLayout(8, 10)); // Adjust grid size to fit timer



    int i = 0;
    int[] x = new int[30];
    int[] y = new int[30];
    int[] z = new int[30];

    while (i < 30) {
        x[i] = rand.nextInt(90) + 10;
        y[i] = rand.nextInt(90) + 10;
        z[i] = rand.nextInt(4);
        if (z[i] == 0 && judgeSame(x, y, z, i)) { // Addition
            answer[i] = x[i] + y[i];
            add(new JLabel(x[i] + " + " + y[i] + " ="));
            password[i] = new JPasswordField();
            password[i].setSize(50,20);
            add(password[i]);
            i++;
        }
        else if (z[i] == 1 && judgeSame(x, y, z, i)) { // Subtraction
            if (x[i] >= y[i]) {
                answer[i] = x[i] - y[i];
                add(new JLabel(x[i] + " - " + y[i] + " ="));
                password[i] = new JPasswordField();
                password[i].setSize(50,20);
                add(password[i]);
                i++;
            }
        }
        else if (z[i] == 2 && judgeSame(x, y, z, i)) { // Multiplication
            if (x[i] * y[i] < 1000) {
                answer[i] = x[i] * y[i];
                add(new JLabel(x[i] + " * " + y[i] + " ="));
                password[i] = new JPasswordField();
                add(password[i]);
                i++;
            }
        }
        else if (z[i] == 3 && judgeSame(x, y, z, i)) {
            if (y[i] != 0 && (x[i] / y[i]) * y[i] == x[i]) {
                answer[i] = x[i] / y[i];
                add(new JLabel(x[i] + " / " + y[i] + " ="));
                password[i] = new JPasswordField();
                add(password[i]);
                i++;
            }
        }
    }

    JButton confirm = new JButton("查看得分");
    confirm.addActionListener(e -> {
        calculateScore();
    });
    add(confirm);



    timerLabel = new JLabel("剩余时间: " + timeLeft + " 秒", SwingConstants.CENTER);
    add(timerLabel);

    timer = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            timeLeft--;
            timerLabel.setText("剩余时间: " + timeLeft + " 秒");
            if (timeLeft <= 0) {
                timer.stop();
                disableInputs();
                calculateScore();
            }
        }
    });
    timer.start();
}

private void disableInputs() {
    for (JPasswordField pass : password) {
        pass.setEnabled(false);
    }
}

public boolean judgeSame(int[] x, int[] y, int[] z, int i) {
    for (int j = 0; j < i; j++) {
        if (x[i] == x[j] && y[i] == y[j] && z[i] == z[j]) return false;
    }
    return true;
}

public boolean judgeAnswer(int answer, JPasswordField password) {
    String a = String.valueOf(answer);
    return password.getText().equals(a);
}

private void calculateScore() {
    rightnum = 0;
    for (int i = 0; i < password.length; i++) {
        if (judgeAnswer(answer[i], password[i])) {
            rightnum++;
        }
    }
    JOptionPane.showMessageDialog(this, "你的得分是: " + rightnum + "/" + password.length);
}

}

import javax.swing.*;

public class text {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Question frame = new Question();
frame.setVisible(true);
});
}
}

posted @   张黎健  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示