9.18 周一总结
今天上午学习了机器人实训,学会了如何组装机器车。
下午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类似的方法,通过获取这两个位置的当前时间,来计算出所用时间(并没有写倒计时,而是仅仅在最后输出了一下回答这三十道题的所用时间)若要解决此问题,仅需要另设一个你想要的倒计时时间,通过每次循环获取时间来进行比较,完成倒计时的功能。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通