四则运算

package cacular;

import java.lang.management.OperatingSystemMXBean;
import java.util.*;
import java.util.zip.CheckedOutputStream;

public class cacular01 {
    Timer timer;
    int seconds; // 倒计时的秒数
   static int t=1;
    public cacular01(int seconds) {
        this.seconds = seconds;
        timer = new Timer();
        timer.schedule(new RemindTask(), 0, 1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
            if (seconds > 0) {
                seconds--;
            }

            else {
                t=0;
                System.out.println("Time's up!");
                timer.cancel(); // 结束倒计时
            }
        }
    }

    public static void main(String[] args) {
        // 创建一个随机数生成器
        Random random = new Random();

        // 创建一个列表来存储30个算术题
        List<String> questions = new ArrayList<>();
         int []answer=new int[30];
        // 生成30个算术题
        for (int i = 0; i < 30; i++) {
            // 随机生成两个两位数
            int num1 = random.nextInt(10)+1 ;
            int num2 = random.nextInt(10)+1 ;

            int t=random.nextInt(100)%4;
            // 如果是减法,保证结果非负
            if (t==1) {
                num1 = Math.max(num1, num2);
            }

            // 如果是除法,保证能整除
            if (t==3) {
                 if(num1%num2!=0||num1==0){
                     i--;
                     continue;
                 }
            }
            if(t==2){
                if(num1*num2>=1000){
                    i--;
                    continue;
                }
            }

            // 生成算术题并添加到列表中
           if(t==0) {
               questions.add(num1 + " " + "+" + " " + num2+" = ");
               answer[i]=num1+num2;
           }
           else if(t==1)  {
               questions.add(num1 + " " + "-" + " " + num2+ " = ");
               answer[i]=num1-num2;
           }
           else if(t==2)  {
               questions.add(num1 + " " + "*" + " " + num2+ " = ");
               answer[i]=num1*num2;
           }
           else if(t==3) {
               questions.add(num1 + " " + "/" + " " + num2+"  =  ");
               answer[i]=num1/num2;
           }
        }
        for(String question:questions){
            System.out.println(question);
        }
         int count=0,worry=0;
        int j=0;

        // 遍历所有的算术题
        for (String question : questions) {
            System.out.println(question);
            System.out.print("你的答案是:");
            Scanner sc = new Scanner(System.in);
            if (sc.nextInt()==answer[j]) {
                System.out.println("回答正确!");
                count++;
            } else {
                System.out.println("回答错误!");
                worry++;
            }
            j++;
            new cacular01(10); // 让我们创建一个10秒的倒计时
            if(t==0) {
                worry=30-count;
                break;
            }
        }

        System.out.printf("错题数为:%d\n",worry);
        System.out.println("正确率为:");
        System.out.printf("%.2f\n",count/30.0);

    }
}
posted @ 2023-09-18 21:19  不会JAVA的小袁  阅读(16)  评论(0编辑  收藏  举报