软件工程个人作业01

一个由.java文件组成的出题器,正在做web版的

思路:

定义一个基类,再定义一个整数,一个正分数继承该类。在每个里面设置各自的成员变量、构造方法和计算方法。在主函数中随机生成。

代码:

shu.java

package cal;

public class shu {
    
}

Calcu.java

package cal;
import java.util.Random;
import java.util.Scanner;

public class Calcu extends shu{
    
    public char fh;
    public int shu1;
    public int shu2;
    public int result;
    
    public char getFh() {
        return fh;
    }
    public int getShu1() {
        return shu1;
    }
    public int getShu2() {
        return shu2;
    }
    
    public Calcu()
    {
        char [] fu = new char [4];
        fu[0] = '/';
        fu[1] = '+';
        fu[2] = '-';
        fu[3] = '*';
        fh = fu[new Random().nextInt(3) + 1];
        shu1 = new Random().nextInt(99) + 1;
        shu2 = new Random().nextInt(99) + 1;
        
        if(fh == '+')
            result = shu1 + shu2;
        else if(fh == '-')
            result = shu1 - shu2;
        else if(fh == '*')
            result = shu1 * shu2;
        else
        result = shu1 / shu2;
    }
    
    @SuppressWarnings("resource")
    public String test_zs() {
        Calcu c = new Calcu();
        System.out.print(c.shu1 + " " + c.fh + " " + c.shu2 + " = ");
        int str = new Scanner(System.in).nextInt();
        if(str == c.result)
            return "对!";
        else
            return "错,正确答案为" + c.result;
    }

}

Cal_zs.java:

package cal;

import java.util.Random;
import java.util.Scanner;

public class Cal_zfs extends shu{
    public char fh;
    public String shu1;
    public String shu2;
    public String result;
    
    public char getFh() {
        return fh;
    }

    public String getShu1() {
        return shu1;
    }

    public String getShu2() {
        return shu2;
    }

    public Cal_zfs()
    {
        int fzi1;
        int fmu1;
        int fzi2;
        int fmu2;
        char [] fu = new char [4];
        fu[0] = '/';
        fu[1] = '+';
        fu[2] = '-';
        fu[3] = '*';
        fh = fu[new Random().nextInt(4)];
        
        fmu1 = new Random().nextInt(9) + 1;
        fzi1 = new Random().nextInt(9) + 1;
        while (fzi1 > fmu1)
        {
            fzi1 = new Random().nextInt(9) + 1;
        }
        fmu2 = new Random().nextInt(9) + 1;
        fzi2 = new Random().nextInt(9) + 1;
        while (fzi2 > fmu2)
        {
            fzi2 = new Random().nextInt(9) + 1;
        }
        
        int gy = gys(fzi1, fmu1);
        int gy2 = gys(fzi2, fmu2);
        if(fzi1 / gy == fmu1 / gy)
            shu1 = "1";
        else
            shu1 = fzi1 / gy + "/" + fmu1 / gy;
        if(fzi2 / gy2 == fmu2 / gy2)
            shu2 = "1";
        else
            shu2 = fzi2 / gy2 + "/" + fmu2 / gy2;
        
        int fm = 0, fz = 0;
        if(fh == '+')
        {
            fm = fmu1 * fmu2;
            fz = fmu1 * fzi2 + fmu2 * fzi1;
        }
        else if(fh == '-')
        {
            fm = fmu1 * fmu2;
            fz = fmu2 * fzi1 - fmu1 * fzi2;
        }
        else if(fh == '*')
        {
            fm = fmu1 * fmu2;
            fz = fzi1 * fzi2;
        }
        else
        {
            fm = fmu1 * fzi2;
            fz = fzi1 * fmu2;
        }
        
        int g = gys(fz, fm);
        result = fz / g + "/" + fm / g;
    }
    
    public int gys(int s1, int s2)
    {
        int gy = 1;
        for(int i = 1; i <= (s1 < s2 ? s1 : s2); i++)
        {
            if(s1 % i == 0 && s2 % i == 0)
                gy = i;
        }
        return gy;
    }
    
    @SuppressWarnings("resource")
    public String test_zfs() {
        Cal_zfs c = new Cal_zfs();
        System.out.print(c.shu1 + " " + c.fh + " " + c.shu2 + " = ");
        String str = new Scanner(System.in).next();
        if(str.equals(c.result))
            return "对!";
        else
            return "错,正确答案为" + c.result;
    }

}

Test.java:

package cal;

import java.util.Random;
import java.util.Scanner;

public class Test {
    
    @SuppressWarnings({ "resource", "finally" })
    public static void main(String[] args) {
        int N = 0;
        System.out.print("请输入生成题数:");
        N = new Scanner(System.in).nextInt();
        
        System.out.println("开始答题,请注意分数的最简化");
        shu []c = new shu[N];
        for(int i = 0; i < N; i++)
        {
            int ran = new Random().nextInt(99) + 1;
            if(ran % 2 == 0)
            {
                c[i] = new Calcu();
                for(int j = 0;j < i;j++)
                {
                    try{
                        if(((Calcu) c[i]).getShu1() == ((Calcu) c[j]).getShu1() && ((Calcu) c[i]).getShu2() == ((Calcu) c[j]).getShu2()
                                && ((Calcu) c[i]).getFh() == ((Calcu) c[j]).getFh())
                        {
                            c[i] = new Calcu();
                            j = 0;
                        }
                    }
                    catch(Exception e)
                    {}
                    finally{
                        continue;
                    }
                }
                System.out.println(((Calcu) c[i]).test_zs());
            }
            else
            {
                c[i] = new Cal_zfs();
                for(int j = 0;j < i;j++)
                {
                    try{
                        if(((Cal_zfs) c[i]).getShu1().equals(((Cal_zfs) c[j]).getShu1())&& ((Cal_zfs) c[i]).getShu2().equals(((Cal_zfs) c[j]).getShu2())
                                && ((Cal_zfs) c[i]).getFh() == ((Cal_zfs) c[j]).getFh())
                        {
                            c[i] = new Cal_zfs();
                            j = 0;
                        }
                    }
                    catch(Exception e)
                    {}
                    finally{
                        continue;
                    }
                }
                System.out.println(((Cal_zfs) c[i]).test_zfs());
            }
        }
    }

}

 

结果截图:

 

posted @ 2017-03-02 12:49  丶枫  阅读(254)  评论(0编辑  收藏  举报