Right-BICEP要求设计四则运算2

import java.util.*;
public class Operation2 {
    public static int SIZE(int size)//定制数量
    {
        int q;
        q=size;
        return q;
    }
    
    public static int SecondOperation(String p1)//是否有乘除法
    {
        int q = 4;
        if(p1.equals("Y"))
        {
            q=4;
        } 
        if(p1.equals("N"))
        {
            q=2;
        }
        return q;
    }
    
    public static String Negative(String p2)//加减有无负数
    {
        String q;
        q=p2;
        return q;
    }
    
    public static String Remainder(String p3)//除法有无余数
    {
        String q;
        q=p3;
        return q;
    }
    
    public static int Max(int max)//最大数
    {
        int m;
        m=max;
        return m;
    }
    public static int Min(int min)//最小数
    {
        int m;
        m=min;
        return m;
    }
    
    public static void Display(int SIZE,int SecondOperation,String Negative,String Remainder,int Max,int Min)//算式计算
    {
        String Again[][]=new String[SIZE][1];//用数组装算式,用以判断是否重复
        for(int i=0;i<SIZE;i++)//重复次数,用以确定算式多少
        {        
            int cha=Max-Min;
            int c,q1,w1,q2,w2;
            String s1=new String();
            String s2=new String();
            String equation=new String();       
            String symbol=new String();//符号判定
            c=(int)(Math.random()*SecondOperation);
            if(c==0) symbol="+";
            if(c==1) symbol="-";
            if(c==2) symbol="*";
            if(c==3) symbol="/";     
            for(int j=0;j<2;j++)//两次循环,第一次为第一个数字,第二次为第二个数字
            {
                int n1 =-99999,n2=-99999;//用于后面是否为分数的判定
                int s=(int)(Math.random()*2);//随机数判定整数或分数,0整数,1分数
                if(s==0)//整数
                {
                    if(Negative.equals("N"))
                    {
                        if(c==0||c==1)
                        {
                            while(n1<0)
                            {
                                n1=(int)(Min+Math.random()*(cha+1));//随机产生从min到max之间的数
                            }
                        }
                        else
                        {
                            n1=(int)(Min+Math.random()*(cha+1));
                        }
                    }
                    if(Negative.equals("Y"))
                    {
                        n1=(int)(Min+Math.random()*(cha+1));
                    }
                }
                if(s==1)//分数
                {
                    n1=(int)(Min+Math.random()*(cha+1));//随机产生从min到max之间的数
                    n2=(int)(Min+Math.random()*(cha+1));
                        if(Negative.equals("N"))
                        {
                            if(c==0||c==1)
                            {
                                while(n1<=0||n2<=0)
                                {
                                    n1=(int)(Min+Math.random()*(cha+1));
                                    n2=(int)(Min+Math.random()*(cha+1));
                                }
                            }
                            else
                            {
                                while(n1==0||n2==0)//分母不能为零
                                {
                                    n1=(int)(Min+Math.random()*(cha+1));
                                    n2=(int)(Min+Math.random()*(cha+1));
                                }
                            }
                        }
                        if(Negative.equals("Y"))
                        {
                            while(n1==0||n2==0)//分母不能为零
                            {
                                n1=(int)(Min+Math.random()*(cha+1));
                                n2=(int)(Min+Math.random()*(cha+1));
                            }
                        }
                    int z1=Math.abs(n1),z2=Math.abs(n2);//取n1和n2的绝对值,以便化简输出
                    int chushu=2;
                    if(z1>z2)//化简
                    {
                        while(z2>=chushu)
                        {
                            if(z1%chushu==0&&z2%chushu==0)
                            {
                                z1=z1/chushu;z2=z2/chushu;
                            }
                            else
                            {
                                chushu++;
                            }
                        }                    
                    }
                    else if(z1<z2)
                    {
                        while(z1>=chushu)
                        {
                            if(z1%chushu==0&&z2%chushu==0)
                            {
                                z1=z1/chushu;z2=z2/chushu;
                            }
                            else
                            {
                                chushu++;
                            }
                        }
                    }
                    else
                    {
                        z1=1;z2=1;
                    };
                    if(n1<0)//去掉绝对值,返回原来的数          
                    {
                        n1=0-z1;
                    }
                    else 
                    {
                        n1=z1;
                    }
                    if(n2<0)
                    {
                        n2=0-z2;
                    }
                    else 
                    {
                        n2=z2;
                    }
                }
                if(j==0)//第一个数字
                {
                    q1=n1;w1=n2;
                    if(w1==-1&&q1<0)
                    {
                        q1=Math.abs(q1);
                    }
                    if(w1==-1&&q1>0)
                    {
                        q1=0-q1;
                    }
                    if(w1>-99999)//如果存在分母,则为分数
                    {
                        if(Math.abs(w1)!=1)
                        {
                            if(q1<0&&w1<0)
                            {
                                q1=Math.abs(q1);w1=Math.abs(w1);
                            }
                            if(w1<0)
                            {
                                s1="("+q1+"/("+w1+"))"+"";
                            }
                            else
                            {
                                s1="("+q1+"/"+w1+")"+"";
                            }
                        }
                        if(Math.abs(w1)==1)//为整数
                        {
                            if(q1>=0)
                            {
                                s1=q1+"";
                            }
                            if(q1<0)
                            {
                                s1="("+q1+")"+"";
                            }
                        }
                    }
                    
                    else//否则为整数
                    {
                        if(q1>=0)
                        {
                            s1=q1+"";
                        }
                        if(q1<0)
                        {
                            s1="("+q1+")"+"";
                        }
                    }
                }
                if(j==1)//第二个数字
                {
                    q2=n1;w2=n2;
                    if(c==3)//当为除法时,除数不能为0,
                    {
                        while(q2==0)
                        {
                            q2=(int)(Min+Math.random()*(cha+1));
                        }//分子或整数不能为0
                    }
                    if(w2==-1&&q2<0)
                    {
                        q2=Math.abs(q2);
                    }
                    if(w2==-1&&q2>0)
                    {
                        q2=0-q2;
                    }
                    if(w2>-99999)//如果存在分母,则为分数
                    {
                        if(Math.abs(w2)!=1)
                        {
                            if(q2<0&&w2<0)
                            {
                                q2=Math.abs(q2);w2=Math.abs(w2);
                            }
                            if(w2<0)
                            {
                                s2="("+q2+"/("+w2+"))"+"";
                            }
                            else
                            {
                                s2="("+q2+"/"+w2+")"+"";
                            }
                        }
                        else
                        {
                            if(q2<0)
                            {
                                s2="("+q2+")"+"";
                            }
                            if(q2>0)
                            {
                                s2=q2+"";
                            }
                        }
                    }
                    else//否则为整数
                    {
                        if(q2<0)
                        {
                            s2="("+q2+")"+"";
                        }
                        if(q2>0)
                        {
                            s2=q2+"";
                        }
                    }
                    if(c==3&&Remainder.equals("N"))//除法无余数
                    {
                        q1=q2*n1;w1=w2;
                        if(w1%n1==0)
                        {
                            w1=w1/n1;q1=q1/n1;
                        }
                        if(w1>-99999)//如果存在分母,则为分数
                        {
                            if(Math.abs(w1)!=1)
                            {s1="("+q1+"/"+w1+")"+"";}
                            else//否则为整数
                            {
                                s1=q1+"";
                            }
                        }
                        else//否则为整数
                        {
                            s1=q1+"";
                        }
                        
                    }
                }
            }
            equation=equation+s1+symbol+s2+"=";//表达式
            Again[i][0]=equation;
            for(int k=0;k<(i+1);k++)//避免重复
            {
                if(Again[i][0].equals(Again[k][0]))
                {
                    k--;break;
                }
            }
            System.out.println(equation);
            }
        }  
    public static void main(String args[])
    {
        String p3="Y";
        @SuppressWarnings("resource")
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入定制数量:");
        int size=sc.nextInt();
        System.out.println("是否有乘除法,有(Y),没有(N)");
        String p1=sc.next();
        System.out.println("加减有无负数,有(Y),没有(N)");
        String p2=sc.next();
        if(p1.equals("Y"))
        {
            System.out.println("除法有无余数,有(Y),没有(N)");
            p3=sc.next();
        }
        System.out.println("请输入数值范围最大值:");
        int max=sc.nextInt();
        System.out.println("请输入数值范围最小值:");
        int min=sc.nextInt();        
        int a=SIZE(size);
        int b=SecondOperation(p1) ;
        String c=Negative(p2);
        String d=Remainder(p3);
        int e=Max(max);
        int f=Min(min);
        
        Display(a,b,c,d,e,f);//函数的调用,实现
    }
}

 

结果截图:

有乘除法,加减有负数,除法有余数

有乘除法,加减无负数,除法有余数

有乘除法,加减无负数,除法无余数

五乘除法,加减有负数

无乘除法,加减无负数

posted @ 2016-03-26 19:13  会飞的鱼摆摆  阅读(147)  评论(0编辑  收藏  举报