首页 > 其他分享 >10月14日

10月14日

时间:2024-10-14 20:32:31浏览次数:7  
标签:10 14 num2 int nextInt result operator num1

在原有代码的基础上添加了年级分类

package guv;

import java.util.Random;
import java.util.Scanner;
import java.time.Duration;
import java.time.Instant;

class ArithmeticGenerator {
protected Random random;
protected Scanner scanner;
protected int correctCount;
protected int wrongCount;
protected int n;
protected int max = 30;
protected int totalTimeLimitSeconds = 60;
protected Instant startTime;
protected Instant endTime;

public ArithmeticGenerator() {
    random = new Random();
    scanner = new Scanner(System.in);
    correctCount = 0;
    wrongCount = 0;
    startTime = Instant.now();
    endTime = startTime.plusSeconds(totalTimeLimitSeconds);
}

public void generateAndCheckAnswer() {
    // 需要在子类中实现具体的生成题目和检查答案逻辑
}

public void displayResult() {
    double totalQuestions = 30;
    double correctRate = correctCount / totalQuestions * 100;
    System.out.println("你答对了 " + correctCount + " 道题。");
    System.out.println("你答错了 " + wrongCount + " 道题。");
    System.out.println("正确率为:" + correctRate + "%");
}

}

class SecondGradeArithmeticGenerator extends ArithmeticGenerator {

public void generateAndCheckAnswer() {
    for (int i = 0; i < max; i++) {
        int num1, num2, result;
        String operator;

        num1 = random.nextInt(100) + 1;
        num2 = random.nextInt(100) + 1;
        int operatorIndex = random.nextInt(4);
        switch (operatorIndex) {
            case 0:
                operator = "+";
                result = num1 + num2;
                break;
            case 1:
                num1 = random.nextInt(100) + 1;
                num2 = random.nextInt(num1) + 1;
                operator = "-";
                if ((num1 - num2) < 0) {
                    i--;
                    continue;
                }
                result = num1 - num2;
                break;
            case 2:
                num1 = random.nextInt(100) + 1;
                num2 = random.nextInt(100) + 1;
                operator = "*";
                result = num1 * num2;
                break;
            default:
                operator = "/";
                if (num2 == 0 || num1 % num2!= 0) {
                    i--;
                    continue;
                }
                result = num1 / num2;
                break;
        }

        System.out.println(num1 + " " + operator + " " + num2 + " = ");

        int userAnswer;
        try {
            userAnswer = scanner.nextInt();
        } catch (Exception e) {
            userAnswer = -1;
        }

        if (userAnswer == result) {
            correctCount++;
        } else {
            wrongCount++;
        }

        Instant currentTime = Instant.now();
        if (currentTime.isAfter(endTime)) {
            break;
        }
    }
}

}

class ThirdGradeArithmeticGenerator extends SecondGradeArithmeticGenerator {

public void generateAndCheckAnswer() {
    for (int i = 0; i < max; i++) {
        int num1, num2, num3, num4;
        int result;
        String operator;

        num1 = random.nextInt(1000) + 1;
        num2 = random.nextInt(1000) + 1;
        num3 = random.nextInt(1000) + 1;
        num4 = random.nextInt(1000) + 1;

        int operatorIndex = random.nextInt(4);
        switch (operatorIndex) {
            case 0:
                operator = "+";
                result = num1 + num2 + num3 + num4;
                break;
            case 1:
                operator = "-";
                result = num1 - num2 - num3 - num4;
                if (result < 0) {
                    i--;
                    continue;
                }
                break;
            case 2:
                operator = "*";
                result = num1 * num2 * num3 * num4;
                break;
            default:
                operator = "/";
                if (num2 == 0 || num3 == 0 || num4 == 0 || num1 % num2 % num3 % num4!= 0) {
                    i--;
                    continue;
                }
                result = num1 / num2 / num3 / num4;
                break;
        }

        System.out.println(num1 + " " + operator + " " + num2 + " " + operator + " " + num3 + " " + operator + " " + num4 + " = ");

        int userAnswer;
        try {
            userAnswer = scanner.nextInt();
        } catch (Exception e) {
            userAnswer = -1;
        }

        if (userAnswer == result) {
            correctCount++;
        } else {
            wrongCount++;
        }

        Instant currentTime = Instant.now();
        if (currentTime.isAfter(endTime)) {
            break;
        }
    }
}

}

class FourthGradeArithmeticGenerator extends ThirdGradeArithmeticGenerator {

public void generateAndCheckAnswer() {
    for (int i = 0; i < max; i++) {
        int num1, num2, num3, num4, num5 = 0;
        int result;
        String operator;

        num1 = random.nextInt(1000) + 1;
        num2 = random.nextInt(1000) + 1;
        num3 = random.nextInt(1000) + 1;
        num4 = random.nextInt(1000) + 1;

        int operatorIndex = random.nextInt(4);
        switch (operatorIndex) {
            case 0:
                operator = "+";
                result = num1 + num2 + num3 + num4 + num5;
                break;
            case 1:
                operator = "-";
                result = num1 - num2 - num3 - num4 - num5;
                if (result < 0) {
                    i--;
                    continue;
                }
                break;
            case 2:
                operator = "*";
                result = num1 * num2 * num3 * num4 * num5;
                break;
            default:
                operator = "/";
                if (num2 == 0 || num3 == 0 || num4 == 0 || num5 ==0 || num1 % num2 % num3 % num4 % num5!= 0) {
                    i--;
                    continue;
                }
                result = num1 / num2 / num3 / num4 /num5;
                break;
        }

        System.out.println(num1 + " " + operator + " " + num2 + " " + operator + " " + num3 + " " + operator + " " + num4 + 
        		 " " +operator + " " + num5+" = ");

        int userAnswer;
        try {
            userAnswer = scanner.nextInt();
        } catch (Exception e) {
            userAnswer = -1;
        }

        if (userAnswer == result) {
            correctCount++;
        } else {
            wrongCount++;
        }

        Instant currentTime = Instant.now();
        if (currentTime.isAfter(endTime)) {
            break;
        }
    }
}

}

public class RandomArithmeticGenerator {
public static void main(String[] args) {
System.out.println("请选择年级(1 表示一年级,2 表示二年级,3 表示三年级):");
Scanner scanner = new Scanner(System.in);
int grade = scanner.nextInt();

    ArithmeticGenerator generator;
    if (grade == 2) {
        generator = new SecondGradeArithmeticGenerator();
    } else if (grade == 3) {
        generator = new ThirdGradeArithmeticGenerator();
    } else {
        generator = new ArithmeticGenerator();
    }

    generator.generateAndCheckAnswer();
    generator.displayResult();

    scanner.close();
}

}

标签:10,14,num2,int,nextInt,result,operator,num1
From: https://www.cnblogs.com/jackson12138/p/18466039

相关文章

  • C学习笔记 基础算法整理 (10.9 - )(正学习,持续更新中)
    本文涵盖了适合初学者学习的基础、经典算法。包括:递归递推、排序、搜索/查找、枚举、图/树遍历、动态规划等。推荐了解  C语言各部分基本知识  后进行学习。学习使用算法,可以:了解针对某类问题的通用解决方案提高逻辑思维能力将复杂的任务分解为简单的步骤精简代码,避免......
  • 【JPCS独立出版 | ISSN:1742-6596 | 往届均稳定EI检索】第九届计算机技术与机械电气工
    第九届计算机技术与机械电气工程国际学术论坛(ISCME2024)将于2024年11月8-10日在中国南京隆重召开。本次论坛将围绕“计算机技术”、“机械电气工程”等多个学术领域进行深度探讨,旨在融合各专业的最新研究成果,以促进相关学科和行业的创新与发展。会议将汇聚来自全球的学者......
  • [20241013]sqlplus spool与文件覆盖.txt
    [20241013]sqlplusspool与文件覆盖.txt--//这个问题在8月份遇到的问题,我发现在sqlplus下spoola.sql文件,并没有在当前目录产生a.sql文件,后来我发现建立在环境变量--//ORACLE_PATH定义的目录下,当时以为自己打开多个会话,没有注意自己工作的当前目录。事后我测试,问题视乎消失了,我再......
  • 2024.10.13 模拟赛
    2024.10.13模拟赛T1「KDOI-10」商店砍价赛时直接口胡出一个错误的贪心。一开始的想法是按照\(v[i]\)排序,然后假设输入的长度为\(n\)位。那么对于排序后\(n-5\)位直接选择操作一。对于剩下的,直接bdfs所有情况选答案,复杂度\(O(n\logn)\)。貌似可行,结果随便一个数据......
  • 10月14日记录
    java编程实现四则运算;要求:1.区分二年级三年级,二年级两个100内操作数,三年级不超过四个1000内操作数;随机加减乘除2.实现计数,判断正误点击查看代码importjava.util.*;abstractclassMathProblem{protectedintmaxOperands;//最大操作数protectedbooleanallow......
  • 2024/10/13 模拟赛总结
    人机体检,\(0+0+0+0=0\),打代码源去了#A.一般图最小匹配下次看到这种范围一定要想到dp啊,令\(dp_{i,j}\)为前\(i\)个元素选了\(j\)对点的最小代价由于边权是绝对值,可以对原数组排一遍序,选取的两个点就一定在排序后数组的相邻节点那么就可以得出式子:\(dp_{i,j}=\min\{dp_......
  • 2024.10.14 test
    B平面上有\(n\)个点以及\(k\)条未知的平行线,每个点都分属一条线,每条线都有至少\(2\)点。给出一种方案。\(n\le4e4,k\le50\)。每个点分属一条线的条件非常重要。考虑利用鸽巢原理。考虑取出\(k+1\)个没有两对点同斜率的点,那么,至少有两个点在一条线上,那么就可以确定斜......
  • 【2024潇湘夜雨】WIN10_Ent-G_22H2.19045.5011软件选装纯净特别版10.14
    【系统简介】=============================================================1.本次更新母盘来自WIN10_Ent-G_22H2.19045.5011.进桌面后稍等片刻,等待后续部分优化完成。2.全程离线精简、无人值守调用优化处理制作。部分优化适配系统可能要重启几次,即使显示适配失败也不要在意,可能......
  • 题解:P11145 Strange Homura Game
    ProblemLinkStrangeHomuraGame题意让你猜测一个数\(n\),你只能输出两次,每次输出一个数\(x\),返回\(x\bmodn\)。Solution令输入的数为\(A,B\),输出的数为\(a,b\),答案为\(n\)。一开始想的是CRT,但只能询问\(2\)次。发现输入的值是经过\(\bmodn\)的,已知\((A-a)......
  • 题解:P11063 【MX-X4-T3】「Jason-1」数对变换
    ProblemLink【MX-X4-T3】「Jason-1」数对变换题外话场上把贪心注重在一个奇怪地方了,导致交的时候已经有\(9\)个人\(\textcolor{green}{AC}\)了(哭)。题意简述对于数对\((x,y)\),你可以执行以下两种变换:类型1:取\(1\lek\lex\),令\((x,y)\gets(\lfloor\frac{x}{k}......