9.18 周一总结

发布时间 2023-09-18 22:19:47作者: liuxuechao

今天上午学习了机器人实训,学会了如何组装机器车。

下午Java测试题如下

package yunsuan;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.Scanner;

public class Operation {
    public static void main(String[] args) {
        int flag = 0;
        int n = 0;// 统计错题
        Clock start=Clock.systemUTC();
        LocalDateTime now = LocalDateTime.now(start);
        while (flag < 30) {
            // 运算符号和两个数的生成,同时产生答案
            int answer = 0;
            Random r1 = new Random();
            int num1 = r1.nextInt(0, 100);
            Random r3 = new Random();
            int num3 = r3.nextInt(0, 100);
            Random r2 = new Random();
            int num2 = r2.nextInt(0, 4);
            char c = ' ';
            if (num2 == 0) {
                c = '+';
                answer = num1 + num3;
            } else if (num2 == 1) {
                c = '-';
                answer = num1 - num3;
            } else if (num2 == 2) {
                c = '*';
                answer = num1 * num3;
            } else if (num2 == 3) {
                while(num1%num3!=0&&num1!=0)
                {
                num3=r3.nextInt(0,100);
                }
                c = '/';
                answer = num1 / num3;
            }
            // 在以下情况下均不会打印输出
            if (c == '-' && num3 > num1)
                continue;
            else if (c == '*' && num1 * num3 > 999)
                continue;
              else if (c == '/' && (num1 < num3 && num1 % num3 != 0)) continue;             
            else {
                System.out.println(num1 + "" + c + "" + num3 + "=");
                flag += 1;
                Scanner sc = new Scanner(System.in);
                int result = sc.nextInt();
                if (result == answer) {
                    System.out.println("回答正确");
                    continue;
                } else if (result != answer) {
                    System.out.println("回答错误");
                    n += 1;
                    continue;
                }
            }
        }
        float b = (float) ((30.0 - n) / 30.0);
        System.out.println("错题数:" + n + "    " + "正确率:" + b);
        Clock end=Clock.systemUTC();
        LocalDateTime now1 = LocalDateTime.now(end);
        Duration duration = Duration.between(now, now1);  
          
        // 输出时间间隔(以秒为单位)  
        long seconds = duration.getSeconds();  
        System.out.println("用时"+seconds+"秒");

    }
}

首先随机生成三个数,其中两个0-100,一个0-3负责选择运算符;

针对相减不可以等于负数,相乘不能超过三位数,相除是整数,定义了一个flag,令他小于三十作为while循环控制语句。

在运算符选择时,定义了一个answer进行运算结果的记录,以便在最后输出回答是否正确,以及定义n来记录错题数量

对于倒计时问题,用了clock这一个和c++中Clock_t类似的方法,通过获取这两个位置的当前时间,来计算出所用时间(并没有写倒计时,而是仅仅在最后输出了一下回答这三十道题的所用时间)若要解决此问题,仅需要另设一个你想要的倒计时时间,通过每次循环获取时间来进行比较,完成倒计时的功能。