课后作业

JAVA方法课后实验问题
方法重载

方法名相同,参数状态不同构成重载函数。调用square函数时参数为int,得到整数参数的平方值,参数为double,得到小数的平方值。
课后作业
代码
package test01;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class rand {
static int[] s = new int[30];
static int[] t = new int[30];
static int[] p = new int[30];
static int tem1, tem2, tem3, q = 0;
static Scanner in = new Scanner(System.in);
static int remainingTime; // 剩余时间(秒)
static boolean timeUp = false; // 时间是否用完

// 倒计时任务
static class CountdownTask extends TimerTask {
    @Override
    public void run() {
        remainingTime--;
        if (remainingTime <= 0) {
            remainingTime = 0;
            timeUp = true;
            System.out.println("\n时间到!");
            this.cancel(); // 取消计时器
        }
        // 显示剩余时间(每分钟显示一次或时间不多时更频繁)
        if (remainingTime % 60 == 0 || remainingTime <= 30) {
            System.out.println("剩余时间:" + formatTime(remainingTime));
        }
    }
}

// 格式化时间为分:秒
private static String formatTime(int seconds) {
    long minutes = TimeUnit.SECONDS.toMinutes(seconds);
    long secs = seconds - TimeUnit.MINUTES.toSeconds(minutes);
    return String.format("%d分%d秒", minutes, secs);
}

public static void ann(int a, int b) {
    System.out.println(a + "+" + b + "=");
    int c = in.nextInt();
    if (c == (a + b)) {
        System.out.println("正确");
        q++;
    } else {
        System.out.println("错误,正确结果是:" + (a + b));
    }
}

public static void kk(int a, int b) {
    if (a < b) {
        b = (int) (a * Math.random());
    }
    System.out.println(a + "-" + b + "=");
    int c = in.nextInt();
    if (c == (a - b)) {
        System.out.println("正确");
        q++;
    } else {
        System.out.println("错误,正确结果是:" + (a - b));
    }
}

public static void ch(int a, int b) {
    System.out.println(a + "*" + b + "=");
    int c = in.nextInt();
    if (c == (a * b)) {
        System.out.println("正确");
        q++;
    } else {
        System.out.println("错误,正确结果是:" + (a * b));
    }
}

public static void sh(int a, int b) {
	b=b/2;
    while(a % b != 0) {
            a = b * (int)((Math.random() * 10) + 1);
    }
    System.out.println(a + "/" + b + "=");
    int c = in.nextInt();
    if (c == (a / b)) {
        System.out.println("正确");
        q++;
    } else {
        System.out.println("错误,正确结果是:" + (a / b));
    }
}
public static void crr() {
    int a = (int) (Math.random() * 100); // 随机生成0-99的数a
    int b = (int) (Math.random() * 100); // 随机生成0-99的数b
    tem1 = a;
    tem2 = b;
    // 防止除法时b为0(避免除零异常)
    while (b == 0) {
        b = (int) (Math.random() * 100);
    }
}

public static void cad() {
    crr();
    // 随机生成1-4的数,对应“加、减、乘、除”四种运算
    int d = (int) (Math.random() * 4) + 1;
    tem3 = d;
    switch (d) {
        case 1: // 加法
            ann(tem1, tem2);
            break;
        case 2: // 减法
            kk(tem1, tem2);
            break;
        case 3: // 乘法
            while (tem1 * tem2 >= 1000) {
                crr();
            }
            ch(tem1, tem2);
            break;
        case 4: // 除法(整数除法)
            sh(tem1, tem2);
            break;
        default:
            System.out.println("运算类型生成异常");
    }
}

public static void main(String[] args) {
    // 设置倒计时时间为5分钟(300秒)
    remainingTime = 300;
    System.out.println("数学练习开始!你有" + formatTime(remainingTime) + "的时间完成30道题目。");
    
    // 启动计时器
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new CountdownTask(), 0, 1000); // 每秒执行一次
    
    int df = 30;
    int currentQuestion = 0; // 当前题目索引
    
    while (df > 0 && !timeUp) {
        int z = 0;
        rand.cad();
        
        // 检查是否与之前题目重复
        for (int j = 0; j < currentQuestion; j++) {
            if (s[currentQuestion] == s[j] && t[currentQuestion] == t[j] && p[currentQuestion] == p[j]) {
                z = 1;
                break;
            }
        }
        
        if (z == 0) {
            s[currentQuestion] = tem1;
            t[currentQuestion] = tem2;
            p[currentQuestion] = tem3;
            currentQuestion++;
            df--;
            System.out.println("已完成 " + (30 - df) + "/30 题");
        } else {
            z = 0;
        }
    }
    
    // 取消计时器
    timer.cancel();
    
    // 显示结果
    int completed = 30 - df;
    System.out.println("\n练习结束!");
    System.out.println("完成题目数:" + completed);
    System.out.println("正确题数:" + q);
    System.out.println("错题数:" + (completed - q));
    if (completed > 0) {
        System.out.println("正确率:" + (q * 100 / completed) + "%");
    } else {
        System.out.println("正确率:0%");
    }
    
    in.close();
}

}

posted @ 2025-10-04 15:27  康栋  阅读(1)  评论(0)    收藏  举报