2022年java第二次随堂测试(部分问题尚未解决)

四则运算课堂测试二
阶段1:
1、定义方法验证题目是否重复;并将出题参数用子方法定义。
2、定义随机数生成器子方法,根据出题参数(出题个数、操作数的个数、确定操作数的取值范围)生成全部的随机操作数。
阶段2:
1、定义方法实现在线答题;全部完成后,显示答题结果,输出正确率和错题。
2、定义方法实现错题集、错题重练并记录错题的次数功能。
阶段三
3、能处理大数和浮点数计算。

参考:四则运算继承版 - 今天又双叒叕在敲代码 - 博客园 (cnblogs.com)

import java.util.Scanner;
import java.util.Random;
import java.lang.Math;
import java.math.BigDecimal;
import java.math.BigInteger;

class BigDecimalArithUtil {

    private static final int DIV_SCALE = 20;//除法精度(除不尽时保留20为小数)
    
    /** 小数精确加法  */
    public static double add(double d1,double d2)
    {
        BigDecimal bd1 = BigDecimal.valueOf(d1);
        BigDecimal bd2 = BigDecimal.valueOf(d2);
        return bd1.add(bd2).doubleValue();
    }
    
    /** 小数精确减法  */
    public static double sub(double d1,double d2)
    {
        BigDecimal bd1 = BigDecimal.valueOf(d1);
        BigDecimal bd2 = BigDecimal.valueOf(d2);
        return bd1.subtract(bd2).doubleValue();
    }
    
    /** 小数精确乘法  */
    public static double mul(double d1,double d2)
    {
        BigDecimal bd1 = BigDecimal.valueOf(d1);
        BigDecimal bd2 = BigDecimal.valueOf(d2);
        return bd1.multiply(bd2).doubleValue();
    }
    
    /** 小数精确除法   */
    public static double div(double d1,double d2)
    {
        BigDecimal bd1 = BigDecimal.valueOf(d1);
        BigDecimal bd2 = BigDecimal.valueOf(d2);
        /*
         * 当除不尽时,以四舍五入的方式(关于除不尽后的值的处理方式有很多种)保留小数点后10位小数
         */
        return bd1.divide(bd2, DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
public class three {
    static int number_test = 0;//题目数目
    //普通
    static int aa[] = new int[100010];
    static int bb[] = new int[100010];
    static char cc[] = new char[100010];
    static int wrong[] = new int[100010];
    static int Ans[] = new int[100010];
    //浮点
    static double double_aa[] = new double[100010];
    static double double_bb[] = new double[100010];
    static char double_cc[] = new char[100010];
    static double double_wrong[] = new double[100010];
    static double double_Ans[] = new double[100010];
    //大整数
    static BigInteger big_aa[] = new BigInteger[100010];
    static BigInteger big_bb[] = new BigInteger[100010];
    static char big_cc[] = new char[100010];
    static BigInteger big_Ans[] = new BigInteger[100010];
    static BigInteger big_wrong[] = new BigInteger[100010];
    //查重
    public static int check(int[] aa,int [] bb,int i,int j, int n) {
        for(int k = 0; k <= n; k++) {
            if((aa[k] == i) && (bb[k] == j))
                return 0;
        }
        return 1;
    }
    //在线答题
    public static int online_answer(int a, char c, int b,int answer,int youranswer) {
        int flag = 0;
//        if(c == '+')
//            answer = a + b;
//        else if (c == '-')
//            answer =  a - b;
//        else if (c == '*')
//            answer = a * b;
//        else if (c == '/')
//            answer = a / b;
//        
        if (answer == youranswer) {
            flag = 1;
            return flag;
        }
        else {
            System.out.print("错误!\n");
            flag = 0;
            return flag;
        }
        
    }
    //在线答题_浮点数
        public static int double_online_answer(double a, char c, double b,double answer,double youranswer) {
            int flag = 0;
//                    if(c == '+')
//                        answer = a + b;
//                    else if (c == '-')
//                        answer =  a - b;
//                    else if (c == '*')
//                        answer = a * b;
//                    else if (c == '/')
//                        answer = a / b;
//                    
            if (BigDecimalArithUtil.sub(answer, youranswer) < 1e-10) {
                flag = 1;
                return flag;
            }
            else {
                System.out.print("错误!\n");
                flag = 0;
                return flag;
            }
            
        }    
    //在线答题_大整数
        public static int big_online_answer(BigInteger a, char c, BigInteger b,BigInteger answer,BigInteger youranswer) {
            int flag = 0;
//                    if(c == '+')
//                        answer = a + b;
//                    else if (c == '-')
//                        answer =  a - b;
//                    else if (c == '*')
//                        answer = a * b;
//                    else if (c == '/')
//                        answer = a / b;
//            
            String a1 = String.valueOf(answer);
            String a2 = String.valueOf(youranswer) ;
            
//            System.out.println(a1);
//            System.out.println(a2);
            
            if (a1.equals(a2)) {
                flag = 1;
                return flag;
            }
            else {
                System.out.print("错误!\n");
                flag = 0;
                return flag;
            }
            
        }
    //错题集
    public static void wrong_test_retrain(int aa, char cc, int bb, int wrong) {        
        if (wrong == 1) {
            System.out.print(aa);
            System.out.print(cc);
            System.out.print(bb);
            System.out.print("=");
        }        
    }
    //错题重练
    public static int wrong_test_retrain2(int aa, char cc, int bb, int wrong, int answer, int youranswer) {    
        Scanner sc = new Scanner(System.in);
        int flag = 1;
        if (wrong == 1) {
            if (answer == youranswer) {
                System.out.print("正确!\n");
                flag = 1;return flag;
            }
            else {
                System.out.print("又错误!\n");
                System.out.print("正确答案:");
                System.out.print(answer + "\n");
                flag = 0;return flag;
            }
        }
        return flag;
    }
    //错题集_浮点数
    public static void double_wrong_test_retrain(double aa, char cc, double bb, int wrong) {        
        if (wrong == 1) {
            System.out.print(aa);
            System.out.print(cc);
            System.out.print(bb);
            System.out.print("=");
        }        
    }
    //错题重练_浮点数
    public static int double_wrong_test_retrain2(double aa, char cc, double bb, int wrong, double answer, double youranswer) {    
        Scanner sc = new Scanner(System.in);
        int flag = 1;
        if (wrong == 1) {
            if (BigDecimalArithUtil.sub(answer, youranswer) < 1e-10) {
                System.out.print("正确!\n");
                flag = 1;
                return flag;
            }
            else {
                System.out.print("又错误!\n");
                System.out.print("正确答案:");
                System.out.print(answer + "\n");
                flag = 0;
                return flag;
            }
        }
        return flag;
    }
    //错题集_大整数
    public static void big_wrong_test_retrain(BigInteger aa, char cc, BigInteger bb, int wrong) {        
        if (wrong == 1) {
            System.out.print(aa);
            System.out.print(cc);
            System.out.print(bb);
            System.out.print("=");
        }        
    }
    //错题重练_大整数
    public static int big_wrong_test_retrain2(BigInteger aa, char cc, BigInteger bb, int wrong, BigInteger answer, BigInteger youranswer) {    
        Scanner sc = new Scanner(System.in);
        String a1 = String.valueOf(answer);
        String a2 = String.valueOf(youranswer) ;
        int flag = 1;
        if (wrong == 1) {
            if (a1.equals(a2)) {
                System.out.print("正确!\n");
                flag = 1;return flag;
            }
            else {
                System.out.print("又错误!\n");
                System.out.print("正确答案:");
                System.out.print(answer + "\n");
                flag = 0;return flag;
            }
        }
        return flag;
    }
    //
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long t = System.currentTimeMillis(); //获得当前时间的毫秒数
        Random rand = new Random(t);
        int a,b,answer = 0;
        double double_answer = 0.0;
        BigInteger big_answer = BigInteger.valueOf(0);
        int youranswer = 0;
        double double_youranswer = 0.0;
        BigInteger big_youranswer = BigInteger.valueOf(0);
        int right_cnt = 0;//正确答题数目
        int c_rand;
        char c = ' ';
        int number_num = 0;//操作数个数
        int number_range = 0;//范围(最大值)
        int BIG=0;
        System.out.print("请输入题目数目:");
        number_test = sc.nextInt();
        System.out.print("请输入操作数个数:");
        number_num = sc.nextInt();
        System.out.print("请输入范围(最大值):");
        number_range = sc.nextInt();
        System.out.print("计算浮点数或大整数(0.普通数据 1.浮点数 2.大整数)(PS:大整数不包含范围是随机的大整数):");
        BIG = sc.nextInt();
        
        if (BIG == 0) {    
            for (int i = 0; i < number_test; i++) {
                a = rand.nextInt(number_range) + 1;
                b = rand.nextInt(number_range) + 1;
                aa[i] = a;bb[i] = b;
                c_rand = rand.nextInt(100)+1;
                if (c_rand % 4 == 0) {
                    c = '+';cc[i] = c;
                    answer = a + b;Ans[i] = answer;
                }
                else if (c_rand % 4 == 1) {
                    c = '-';cc[i] = c;
                    answer = a - b;Ans[i] = answer;
                }
                else if (c_rand % 4 == 2) {
                    c = '*';cc[i] = c;
                    answer = a * b;Ans[i] = answer;
                }
                else if (c_rand % 4 == 3) {
                    c = '/';cc[i] = c;
                    answer = a / b;Ans[i] = answer;
                }
                
                System.out.print(a);
                System.out.print(c);
                System.out.print(b);
                System.out.print("=");
                System.out.print("\n");
                
                System.out.print("请输入你的答案:");
                youranswer = sc.nextInt();
                
                if(online_answer(a,c,b,answer,youranswer) == 0) {
                    wrong[i] = 1;//标记错误
                }
                else {
                    right_cnt++;
                }
                
            }
            
            System.out.println("正确率:" + (((double)right_cnt/(double)number_test)*100) + "%");
            System.out.print("===错题重练===\n");
            for (int i = 0; i < number_test; i++) {
                int wrong_cnt = 0;//错误次数统计
                wrong_test_retrain(aa[i], cc[i], bb[i], wrong[i]);
                if (wrong[i] == 1) {
                    System.out.print("请输入你的答案:");
                    youranswer = sc.nextInt();
                    if (wrong_test_retrain2(aa[i], cc[i], bb[i], wrong[i],Ans[i],youranswer) == 0) {
                        wrong_cnt++;
                    }
                }
                if (wrong_cnt != 0) {
                    System.out.println("错误次数=" + (wrong_cnt+1));
                }
                System.out.println("");
            }
        }
        else if (BIG == 1) {
            for (int i = 0; i < number_test; i++) {
                double double_a = rand.nextDouble()*number_range;
                double double_b = rand.nextDouble()*number_range;
                
                double_aa[i] = double_a;double_bb[i] = double_b;
                c_rand = rand.nextInt(100)+1;
                if (c_rand % 4 == 0) {
                    c = '+';double_cc[i] = c;
                    double_answer = BigDecimalArithUtil.add(double_a, double_b);
                    double_Ans[i] = double_answer;
                }
                else if (c_rand % 4 == 1) {
                    c = '-';double_cc[i] = c;
                    double_answer = BigDecimalArithUtil.sub(double_a, double_b);
                    double_Ans[i] = double_answer;
                }
                else if (c_rand % 4 == 2) {
                    c = '*';double_cc[i] = c;
                    double_answer = BigDecimalArithUtil.mul(double_a, double_b);
                    double_Ans[i] = double_answer;
                }
                else if (c_rand % 4 == 3) {
                    c = '/';double_cc[i] = c;
                    double_answer = BigDecimalArithUtil.div(double_a, double_b);
                    double_Ans[i] = double_answer;
                }
                
                System.out.print(double_a);
                System.out.print(c);
                System.out.print(double_b);
                System.out.print("=");
                System.out.print("\n");
                
                System.out.print("请输入你的答案:");
                double_youranswer = sc.nextDouble();
                
                if(double_online_answer(double_a,c,double_b,double_answer,double_youranswer) == 0) {
                    wrong[i] = 1;//标记错误
                }
                else {
                    right_cnt++;
                }    
            }
            System.out.println("正确率:" + (((double)right_cnt/(double)number_test)*100) + "%");
            System.out.print("===错题重练===\n");
            for (int i = 0; i < number_test; i++) {
                int wrong_cnt = 0;//错误次数统计
                double_wrong_test_retrain(double_aa[i], double_cc[i], double_bb[i], wrong[i]);
                if (wrong[i] == 1) {
                    System.out.print("请输入你的答案:");
                    youranswer = sc.nextInt();
                    if (double_wrong_test_retrain2(double_aa[i], double_cc[i], double_bb[i], wrong[i],double_Ans[i],double_youranswer) == 0) {
                        wrong_cnt++;
                    }
                }
                if (wrong_cnt != 0) {
                    System.out.println("错误次数=" + (wrong_cnt+1));
                }
                System.out.println("");
            }
        }
        else if (BIG == 2) {
            for (int i = 0; i < number_test; i++) {
                int cnt = rand.nextInt(20)+10;
                int cnt_bits = 0;
                String number_a = "";
                String number_b = "";
                for (int j = 0; j < cnt; j++) {
                    cnt_bits = rand.nextInt(9)+1;
                    number_a += cnt_bits;
                }
                for (int j = 0; j < cnt; j++) {
                    cnt_bits = rand.nextInt(9)+1;
                    number_b += cnt_bits;
                }
                BigInteger big_a = new BigInteger(number_a);
                BigInteger big_b = new BigInteger(number_b);
                
                big_aa[i] = big_a;big_bb[i] = big_b;
                c_rand = rand.nextInt(100)+1;
                if (c_rand % 4 == 0) {
                    c = '+';big_cc[i] = c;
                    big_answer = big_a.add(big_b);
                    big_Ans[i] = big_answer;
                }
                else if (c_rand % 4 == 1) {
                    c = '-';big_cc[i] = c;
                    big_answer = big_a.subtract(big_b);
                    big_Ans[i] = big_answer;
                }
                else if (c_rand % 4 == 2) {
                    c = '*';big_cc[i] = c;
                    big_answer = big_a.multiply(big_b);
                    big_Ans[i] = big_answer;
                }
                else if (c_rand % 4 == 3) {
                    c = '/';big_cc[i] = c;
                    big_answer = big_a.divide(big_b);
                    big_Ans[i] = big_answer;
                }
                
                System.out.print(big_a);
                System.out.print(c);
                System.out.print(big_b);
                System.out.print("=");
                System.out.print("\n");
                
//                System.out.println(big_answer);
                
                System.out.print("请输入你的答案:");
                big_youranswer = sc.nextBigInteger();
                
//                System.out.println(big_answer);
//                System.out.println("");
                
                if(big_online_answer(big_a,c,big_b,big_answer,big_youranswer) == 0) {
                    wrong[i] = 1;//标记错误
                }
                else {
                    right_cnt++;
                }    
            }
            System.out.println("正确率:" + (((double)right_cnt/(double)number_test)*100) + "%");
            System.out.print("===错题重练===\n");
            for (int i = 0; i < number_test; i++) {
                int wrong_cnt = 0;//错误次数统计
                big_wrong_test_retrain(big_aa[i], big_cc[i], big_bb[i], wrong[i]);
                if (wrong[i] == 1) {
                    System.out.print("请输入你的答案:");
                    big_youranswer = sc.nextBigInteger();
                    if (big_wrong_test_retrain2(big_aa[i], big_cc[i], big_bb[i], wrong[i],big_Ans[i],big_youranswer) == 0) {
                        wrong_cnt++;
                    }
                }
                if (wrong_cnt != 0) {
                    System.out.println("错误次数=" + (wrong_cnt+1));
                }
                System.out.println("");
            }
        }
        
    }
}

 

posted @ 2022-09-17 14:44  sodamate  阅读(43)  评论(0编辑  收藏  举报