首页 > 代码库 > 课堂练习三

课堂练习三

        这次的课堂练习具体的思路还是通过方法的调用来完成要求。在显示正确和错误时通过用户每一次输入之后判断正误,然后把正确的题目和错误的题目分别放在不同的数组之中,然后在最后通过数组的输出来显示正确和错误的题目数。检查重复则是通过对题目的左右变换来判断是否重复。至于规定数量以及对题目范围的要求只需要写出一个简单的判断方法即可。无负数只需在题目生成之后判断即可。

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. 用户不对题目做出回答的情况

记录表:

                                     技术分享

                                                   技术分享

                                              技术分享

PSP计划:

                                          技术分享

                                              技术分享

课堂练习三