作业

64k 的文件是什么概念呢?
1 行代码大概(平均)是 30 字节, 64k 的源代码是 2184行;
如果代码风格好一点,再多一些空行的话,差不多也就是 3000 行上下。
Modules(模块):将一个复杂的系统划分为子模块,便于设计、实现和维护;
Java中的程序模块:方法、类、包;
Java程序中最基本的构造单元是类,而类中最重要的成员就是方法。
一个构成递归调用的函数
void DonotRunMe()
{
DonotRunMe();
}
一个递归的算法,会将同一个“大”问题的“规模”不断压缩,直到易于处理为止。往往体现为代码要处理的数据量在递归前后不断“递减”。
每个递归函数的开头一定是判断递归结束条件是否满足的语句(一般是if语句);
函数体一定至少有一句是“自己调用自己”的。
每个递归函数一定有一个控制递归可以终结的变量(通常是作为函数的参数而存在)。每次自己调用自己时,此变量会变化(一般是变小),并传送给被调用的函数。
由于计算机使用固定的位数来保存数值,因此,能处理的数值大小是有限的,当要处理的数值超过了这一范围时,计算机将会自动截断数值的二进制表示为它所能处理的最多位数,这将导致错误的处理结果
一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。

public class LinearCongruentialGenerator {
private long seed;
private final long modulus = 2147483647L; // 2^31 - 1
private final long multiplier = 16807L;
private final long increment = 0L;

public LinearCongruentialGenerator(long seed) {
    this.seed = (seed <= 0 || seed >= modulus) ? System.currentTimeMillis() % modulus : seed;
    if (this.seed == 0) this.seed = 1; 
}

public LinearCongruentialGenerator() {
    this(System.currentTimeMillis());
}
public int nextInt() {
    seed = (multiplier * seed + increment) % modulus;
    return (int) seed;
}
public int[] generateRandomNumbers(int count) {
    if (count <= 0) {
        throw new IllegalArgumentException("数量必须大于0");
    }
    
    int[] randomNumbers = new int[count];
    for (int i = 0; i < count; i++) {
        randomNumbers[i] = nextInt();
    }
    return randomNumbers;
}
public int[] generateRandomNumbersInRange(int count, int min, int max) {
    if (count <= 0) {
        throw new IllegalArgumentException("数量必须大于0");
    }
    if (min >= max) {
        throw new IllegalArgumentException("最小值必须小于最大值");
    }
    
    int[] randomNumbers = new int[count];
    int range = max - min + 1;
    
    for (int i = 0; i < count; i++) {
        randomNumbers[i] = min + (nextInt() % range);
    }
    return randomNumbers;
}

}

import java.util.Random;
import java.util.Scanner;

class math{
private int worry=0;
public static void main(String[] a){
for(int i=1;i<=30;i++){
String problem=Problem();
System.out.println(i + ". " + problem);
}
}

public static String Problem() {
    Random random=new Random();
    int y=random.nextInt(4);
    switch(y){
        case 0:
            return jia();
        case 1:
            return jian();
        case 2:
            return cheng();
        case 3:
            return chu();
        default:
            return jia();
    }
}

public static String jia(){
    Random random=new Random();
    int num1= random.nextInt(100)+1;
    int num2= random.nextInt(100)+1;
    return num1+"+"+num2+"=";
}
public static String jian(){
    Random random=new Random();
    int num1= random.nextInt(100)+1;
    int num2= random.nextInt(num1)+1;
    return num1+"-"+num2+"=";
}
public static String cheng(){
    Random random=new Random();
    int num1= random.nextInt(100)+1;
    int num2= random.nextInt(1000)+1/num1;
    return num1+"*"+num2+"=";
}
public static String chu(){
    Random random=new Random();
    int num2= random.nextInt(100)+1;
    int num1= num2*random.nextInt(100)+1;

    return num1+"/"+num2+"=";
}

}
import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class SimpleMathPractice extends JFrame {
private JLabel questionLabel, timerLabel, resultLabel;
private JTextField answerField;
private JButton submitButton, startButton;

private int currentQuestion = 0;
private int correctCount = 0;
private int totalQuestions = 10; // 减少到10题
private int timeLeft = 300; // 5分钟
private Timer timer;
private String[] questions = new String[totalQuestions];
private int[] answers = new int[totalQuestions];

public SimpleMathPractice() {
    setTitle("数学练习");
    setSize(400, 250);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    
    // 创建组件
    questionLabel = new JLabel("点击开始按钮");
    timerLabel = new JLabel("时间: 5:00");
    resultLabel = new JLabel("正确: 0/" + totalQuestions);
    answerField = new JTextField(10);
    submitButton = new JButton("提交");
    startButton = new JButton("开始");
    
    // 初始状态
    answerField.setEnabled(false);
    submitButton.setEnabled(false);
    
    // 添加到窗口
    add(questionLabel);
    add(answerField);
    add(submitButton);
    add(startButton);
    add(timerLabel);
    add(resultLabel);
    
    // 添加事件
    startButton.addActionListener(e -> startTest());
    submitButton.addActionListener(e -> checkAnswer());
    answerField.addActionListener(e -> checkAnswer());
    
    setVisible(true);
}

private void startTest() {
    generateQuestions();
    currentQuestion = 0;
    correctCount = 0;
    timeLeft = 300;
    
    answerField.setEnabled(true);
    submitButton.setEnabled(true);
    startButton.setEnabled(false);
    
    showQuestion(0);
    startTimer();
    updateResult();
}

private void generateQuestions() {
    Random rand = new Random();
    for (int i = 0; i < totalQuestions; i++) {
        int a = rand.nextInt(50) + 1;
        int b = rand.nextInt(50) + 1;
        int op = rand.nextInt(4);
        
        switch (op) {
            case 0: // 加法
                questions[i] = a + " + " + b + " = ?";
                answers[i] = a + b;
                break;
            case 1: // 减法(确保非负)
                if (a < b) { int temp = a; a = b; b = temp; }
                questions[i] = a + " - " + b + " = ?";
                answers[i] = a - b;
                break;
            case 2: // 乘法(确保不超过三位数)
                if (a > 20) a = 20;
                if (b > 20) b = 20;
                questions[i] = a + " × " + b + " = ?";
                answers[i] = a * b;
                break;
            case 3: // 除法(确保整除)
                b = rand.nextInt(10) + 1;
                answers[i] = rand.nextInt(10) + 1;
                a = b * answers[i];
                questions[i] = a + " ÷ " + b + " = ?";
                break;
        }
    }
}

private void showQuestion(int index) {
    if (index < totalQuestions) {
        questionLabel.setText("第" + (index + 1) + "题: " + questions[index]);
        answerField.setText("");
        answerField.requestFocus();
    }
}

private void checkAnswer() {
    try {
        int userAnswer = Integer.parseInt(answerField.getText());
        if (userAnswer == answers[currentQuestion]) {
            correctCount++;
            JOptionPane.showMessageDialog(this, "正确!");
        } else {
            JOptionPane.showMessageDialog(this, "错误!正确答案是: " + answers[currentQuestion]);
        }
        
        currentQuestion++;
        if (currentQuestion < totalQuestions) {
            showQuestion(currentQuestion);
        } else {
            endTest();
        }
        updateResult();
        
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(this, "请输入数字!");
    }
}

private void startTimer() {
    timer = new Timer(1000, e -> {
        timeLeft--;
        int minutes = timeLeft / 60;
        int seconds = timeLeft % 60;
        timerLabel.setText(String.format("时间: %d:%02d", minutes, seconds));
        
        if (timeLeft <= 0) {
            endTest();
        }
    });
    timer.start();
}
posted @ 2025-10-08 13:13  houjinkang  阅读(5)  评论(0)    收藏  举报