• 四则运算3


    import java.util.*;
    //正数的随机取值
    class number1
    {
        private int i;
        private int j;
        public void seti()
        {
            i = 1 + (int)(Math.random() * 100);
        }
        public int geti()
        {
            return i;
        }
        public void setj()
        {
            j = 1 + (int)(Math.random() * 99);
        }
        public int getj()
        {
            return j;
        }
        
    }
    //负数的随机取值
    class Number
    {
        private int i;
        private int j;
        public void seti()
        {
            i = (-1) + (int)(Math.random() * (-100));
        }
        public int geti()
        {
            return i;
        }
        public void setj()
        {
            j = (-1) + (int)(Math.random() * (-99));
        }
        public int getj()
        {
            return j;
        }
        
    }
    //运算符号的随机取值
    class symbol
    {
        private int a;
        public void seta()
        {
            a = 1 + (int)(Math.random() * 4);
        }
        public int geta()
        {
            return a;
        }
    }
    //分数的随机取值
    class number2
    {
        private int i;
        private int j;
        private int x;
        private int y;
        public void setnumber()
        {
            i = 1 + (int)(Math.random() * 100);
            j = 1 + (int)(Math.random() * 99);
            x = 1 + (int)(Math.random() * 100);
            y = 1 + (int)(Math.random() * 99);
        }
        public int getnumberi()
        {
            return i;
        }
        public int getnumberj()
        {
            return j;
        }
        public int getnumberx()
        {
            return x;
        }
        public int getnumbery()
        {
            return y;
        }    
    }
    //混合运算的取值
    class number3
    {
        private int i;
        private int j;
        private int x;
        private int y;
        public void setnumber()
        {
            i = 1 + (int)(Math.random() * 100);
            j = 1 + (int)(Math.random() * 100);
            x = 1 + (int)(Math.random() * 100);
            y = 1 + (int)(Math.random() * 100);
        }
        public int getnumberi()
        {
            return i;
        }
        public int getnumberj()
        {
            return j;
        }
        public int getnumberx()
        {
            return x;
        }
        public int getnumbery()
        {
            return y;
        }    
    }
    //混合运算的符号
    class symbol1
    {
        private int a;
        public void seta()
        {
            a = 1 + (int)(Math.random() * 2);
        }
        public int geta()
        {
            return a;
        }
    }
    //主函数
    public class calculate {
        public static void main(String args[])
        {
            while(true)
            {
                menu();
                System.out.println("请输入想要运行的计算");
                Scanner scan = new Scanner(System.in);
                int p;
                p = scan.nextInt();
                if(p == 1)
                {
                    positive();
                }
                if(p == 2)
                {
                    negative();
                }
                if(p == 3)
                {
                    fraction();
                }
                    
                if(p == 4)
                {
                    divide();
                }
                if(p == 5)
                {
                    divide1();
                }
                if(p == 6)
                {
                    range();
                }
                if(p == 7)
                {
                    mixture();
                }
            }
        }
        public static String simplify(int a,int b)   //获得最简分数
        {
            int max = 0;
            for(int i = 0 ;i < 100 ;i ++)
            {
                int n = i + 1 ;
                if(a % n == 0 && b % n == 0)
                    max = n;
                
            }
            return (String)( a / max + "/" + b / max);
        }
        public static int charge(int answer) //判断正误
        {
            System.out.println("请输入您的答案:");
            Scanner in = new Scanner(System.in);
            int i;
            i = in.nextInt();
            if(i == answer)
            {
                return 1;
            }
            else return 0;
        }
         public static void menu()
        {
            System.out.println("1. 正数的四则运算");
            System.out.println("2. 负数的四则运算");
            System.out.println("3. 分数的四则运算");
            System.out.println("4. 整除运算");
            System.out.println("5. 非整除运算");
            System.out.println("6. 规定数值范围的运算");
            System.out.println("7. 混合运算");
        }
        public static void positive() //正数运算
        {
            for(int i = 0 ; i <= 29 ; i ++){
                number1 n1 = new number1();
                int a1 , a2;
                n1.seti();
                a1 = n1.geti();
                n1.setj();
                a2 = n1.getj();
                symbol s = new symbol();
                int b;
                s.seta();
                b = s.geta();
                if(b == 1)
                    System.out.println(a1 + "+" + a2 + "=" + ( a1 + a2 ) );
                if(b == 2)
                    System.out.println(a1 + "-" + a2 + "=" + (a1 - a2));
                if(b == 3)
                    System.out.println(a1 + "*" + a2 + "=" + (a1 * a2));
                if(b == 4)
                {
                    int c = a1 % a2;
                    int c1 = (a1 - c) / a2;
                    System.out.println(a1 + "/" + a2 + "=" + c1 + "余" + c);
                }
                    
            }
            
        }
        public static void negative() //负数运算
        {
            for(int i = 0 ; i <= 29 ; i ++){
                Number n1 = new Number();
                int a1 , a2;
                n1.seti();
                a1 = n1.geti();
                n1.setj();
                a2 = n1.getj();
                symbol s = new symbol();
                int b;
                s.seta();
                b = s.geta();
                if(b == 1)
                    System.out.println(a1 + "+" + "(" +a2 + ")" + "=" + ( a1 + a2 ) );
                if(b == 2)
                    System.out.println(a1 + "-" + "(" +a2 + ")" + "=" + (a1 - a2));
                if(b == 3)
                    System.out.println(a1 + "*" + "(" +a2 + ")" + "=" + (a1 * a2));
                if(b == 4){
                    int c = ((-1) * a1) % ((-1) * a2);
                    int c1 = ((-1) * a1 - c) / ((-1) * a2);
                    System.out.println(a1 + "/" + "(" +a2 + ")" + "=" + c1 + "余" + c);
                }
            
            }
        }
        public static void fraction() //分数运算
        {
            for(int i = 0 ; i <= 29 ; i++)
            {
                number2 n2 = new number2();
                int b1 , b2 , b3 , b4;
                n2.setnumber();
                b1 = n2.getnumberi();
                b2 = n2.getnumberj();
                b3 = n2.getnumberx();
                b4 = n2.getnumbery();
                symbol s1 = new symbol();
                int c;
                s1.seta();
                c = s1.geta();
                if(c == 1)
                    System.out.println(simplify(b2 , b1) + "+" + simplify(b4 , b3) + "=" + simplify(b2 * b3 + b4 * b1 , b1 * b3));
                if(c == 2)
                    System.out.println(simplify(b2 , b1) + "-" + simplify(b4 , b3) + "=" + simplify(b2 * b3 - b4 * b1 , b1 * b3));
                if(c == 3)
                    System.out.println(simplify(b2 , b1) + "*" + simplify(b4 , b3) + "=" + simplify(b2 * b4 , b1 * b3));
                if(c == 4)
                    System.out.println(simplify(b2 , b1) + "/" + simplify(b4 , b3) + "=" + simplify(b2 * b3 , b1 * b4));
            }
        }
        public static void divide() //整除运算
        {
            int i = 0;
            while(i < 30){
                number1 n = new number1();
                int a1 , a2;
                n.seti();
                a1 = n.geti();
                n.setj();
                a2 = n.getj();
                if(a1 % a2 == 0)
                {
                    i++;
                    System.out.println(a1 + "/" + a2 + "=" + (a1 / a2));
                }
            }    
        }
        public static void divide1() //不整除运算
        {
            int i = 0;
            while(i < 30){
                number1 n = new number1();
                int a1 , a2;
                n.seti();
                a1 = n.geti();
                n.setj();
                a2 = n.getj();
                if(a1 % a2 != 0)
                {
                    i++;
                    int c = a1 % a2;
                    int c1 = (a1 - c) / a2;
                    System.out.println(a1 + "/" + a2 + "=" + c1 + "余" + c);
                }
            }
        }
        public static void range() //范围运算
        {
            System.out.println("请输入范围的左边界:");
            Scanner scan1 = new Scanner(System.in);
            int a3;
            a3 = scan1.nextInt();
            System.out.println("请输入范围的右边界:");
            Scanner scan2 = new Scanner(System.in);
            int a4;
            a4 = scan1.nextInt();
            int i = 0;
            while(i < 30){
                number1 n1 = new number1();
                int a1 , a2;
                n1.seti();
                a1 = n1.geti();
                n1.setj();
                a2 = n1.getj();
                symbol s = new symbol();
                int b;
                s.seta();
                b = s.geta();
                if(a1 >= a3 && a1 <= a4 && a2 >= a3 && a2 <= a4)
                {
                    i++;
                    if(b == 1)
                        System.out.println(a1 + "+" + a2 + "=" + ( a1 + a2 ) );
                    if(b == 2)
                        System.out.println(a1 + "-" + a2 + "=" + (a1 - a2));
                    if(b == 3)
                        System.out.println(a1 + "*" + a2 + "=" + (a1 * a2));
                    if(b == 4){
                        int c = a1 % a2;
                        int c1 = (a1 - c) / a2;
                        System.out.println(a1 + "/" + a2 + "=" + c1 + "余" + c);
                    }
                }    
            }
        }
        public static void mixture() //混合运算同时能够控制题目的数量,同时也能检查重复,同时还可以判断出回答的正误
        {
            System.out.println("请输入您想要生成的题目数量");
            Scanner in = new Scanner(System.in);
            int  amount;
            amount = in.nextInt();
            int c = 0;
            int[] list =new int[amount + 1];
            int[] list1 = new int[amount + 1];
            int[] list2 = new int[amount + 1];
            while(c < amount)
            {
                number3 n = new number3();
                n.setnumber();
                int b1 , b2 , b3 , b4;
                b1 = n.getnumberi();
                b2 = n.getnumberj();
                b3 = n.getnumberx();
                b4 = n.getnumbery();
                symbol1 s = new symbol1();
                s.seta();
                symbol1 s1 = new symbol1();
                s1.seta();
                symbol1 s2 = new symbol1();
                s2.seta();
                int a1 , a2 , a3;
                a1 = s.geta();
                a2 = s1.geta();
                a3 = s2.geta();
                if(a1 == 1 && a2 == 1 && a3 == 1)
                {
                    if( (b1 + b2 + b3 + b4) >= 0 )
                    {
                        System.out.println(b1 + "+" + b2 + "+" + b3 + "+" + b4 +"=");
                        c ++;
                        if(charge( (b1 + b2 + b3 + b4) ) == 1)
                        {
                            list[c] = c ;
                        }
                        else
                        {
                            list1[c] = c ;
                        }
                    }
                }
                if(a1 == 1 && a2 == 1 && a3 == 2)
                {
                    if((b1 + b2 + b3 - b4) >= 0)
                    {
                        System.out.println(b1 + "+" + b2 + "+" + b3 + "-" + b4 +"=" );
                        c ++;
                        if(charge( (b1 + b2 + b3 - b4) ) == 1)
                        {
                            list[c] = c ;
                        }
                        else
                        {
                            list1[c] = c ;
                        }
                    }
                }
                if(a1 == 1 && a2 == 2 && a3 == 1)
                {
                    if((b1 + b2 - b3 + b4) >= 0)
                    {
                        System.out.println(b1 + "+" + b2 + "-" + b3 + "+" + b4 +"=" );
                        c ++;
                        if(charge( (b1 + b2 - b3 + b4) ) == 1)
                        {
                            list[c] = c ;
                        }
                        else
                        {
                            list1[c] = c ; 
                        }
                    }
                }
                if(a1 == 1 && a2 == 2 && a3 == 2)
                {
                    if((b1 + b2 - b3 - b4) >= 0)
                    {
                        System.out.println(b1 + "+" + b2 + "-" + b3 + "-" + b4 +"=" );
                        c ++;
                        if(charge( (b1 + b2 - b3 - b4) ) == 1)
                        {
                            list[c] = c ;
                        }
                        else
                        {
                            list1[c] = c;
                        }
                    }
                }
                if(a1 == 2 && a2 == 1 && a3 == 1)
                {
                    if((b1 - b2 + b3 + b4) >= 0)
                    {
                        System.out.println(b1 + "-" + b2 + "+" + b3 + "+" + b4 +"=" );
                        c ++;
                        if(charge( (b1 - b2 + b3 + b4) ) == 1)
                        {
                            list[c] = c ;
                        }
                        else
                        {
                            list1[c] = c ;
                        }
                    }
                }
                if(a1 == 2 && a2 == 1 && a3 == 2)
                {
                    if((b1 - b2 + b3 - b4) >= 0)
                    {
                        System.out.println(b1 + "-" + b2 + "+" + b3 + "-" + b4 +"=" );
                        c ++;
                        if(charge( (b1 - b2 + b3 - b4) ) == 1)
                        {
                            list[c] = c ;
                        }
                        else
                        {
                            list1[c] = c ;
                        }
                    }
                }
                if(a1 == 2 && a2 == 2 && a3 == 1)
                {
                    if((b1 - b2 - b3 + b4) >= 0)
                    {
                        System.out.println(b1 + "-" + b2 + "-" + b3 + "+" + b4 +"=" );
                        c ++;
                        if(charge( (b1 - b2 - b3 + b4) ) == 1)
                        {
                            list[c] = c ;
                        }
                        else
                        {
                            list1[c] = c ;
                        }
                    }
                }
                if(a1 == 2 && a2 == 2 && a3 == 2)
                {
                    if((b1 - b2 - b3 - b4) >= 0)
                    {
                        System.out.println(b1 + "-" + b2 + "-" + b3 + "-" + b4 +"=" );
                        c ++;
                        if(charge( (b1 - b2 - b3 - b4) ) == 1)
                        {
                            list[c] = c;
                        }
                        else
                        {
                            list1[c] = c ;
                        }
                    }
                }
            }
            System.out.println("答对的题目有:");
            for(int d = 0 ; d <= amount ; d ++)
            {
                if(list[d] != 0)
                {
                    System.out.println(list[d]);
                }
            }
            System.out.println("答错的题目有:");
            for(int e = 0 ; e <= amount ; e ++)
            {
                if(list1[e] != 0)
                {
                    System.out.println(list1[e]);
                }
            }
        }
        
    }


    用户每答一次题判断一次正误,并建立两个数组,分别存储正确而和错误的题目,同事建立数组今次那个查重实验

     

               

                                                                         

    生成10000道题目无报错:

                                                                         

    总结:本次试验难度较大,主要在括号问题难住了很久没有办法解决,其他的方面通过一些代码使自己的程序健壮性更加丰富,能够承受10000道的生成。在运算范围的检验上检验了从1-10000范围下所有的运算。

    使用的测试用例: 1. 100以下,生成5道题目(加减)

                           2. 100以下,生成5道题目(乘除,但是除法无余数)

                           3. 100以下,生成5道题目(乘除,除法有余数)

                           4. 100-1000,生成100道题目(加减乘除均有)

                           5. 10000以上,生成10000道题目(加减)

                           6. 10000以上,生成10000道题目(乘除,余数可能有)

                           7. 用户输入范围时输入负数的情况

                           8. 用户输入题目数量时输入负数的情况

                           9. 用户回答全错或者全对的情况

                         10. 用户不对题目做出回答的情况

     

      听课

      编程

       阅读

       备考

     

     

      日总计

    星期一

     80min

      45min

     

     

     

     

     125min

    星期二

     

      45min

      30min

     

     

     

      75min

    星期三

     

      25min

     

     

     

     

    25min

    星期四

     

     50min

     

     

     

     

     50min

    星期五

     

    30min

     

     

     

     

     30min

    2、

     

    开始时间

    结束时间

    中断时间

     净时间

      活动

      备注

    3月13日

      13:55

     16:10

      10min

     125min

      听课,编程

    四则运算三

    3月14日

      19:00  

     20:20

       5min

     45min

     编程,阅读

    四则运算三

    3月15日

      15:00

     16:00

     

     60min

     编程

    四则运算三

    3月16日

      15:00

     14:00

       10min

     50min

     编程

    四则运算三

    3月17日

      19:00

     19:30 

     

     30min

     编程

     

     

     

     

     

     

     

     

  • 相关阅读:
    v4L2编程
    我的c#开发之路
    JS 获取页面input控件 追加样式属性
    FileUpload控件应用
    C# Winform项目下实现左侧菜单右侧界面显示效果
    在DropDownList 控件绑定数据源中新增一条自定义数据
    在datatable数据中自定义增加一列
    java 实现字符串词频统计
    Hibernate3.x 中BLOB、CLOB 注解配置写法
    lucene 排序
  • 原文地址:https://www.cnblogs.com/xhxdeblog/p/6573335.html
Copyright © 2020-2023  润新知