软件工程第四周进度

四则运算模块化

import java.util.Scanner;
public class NEWSIZE {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入练习量:");
         int m=sc.nextInt();
         System.out.println("请输入数值的范围:");
         int sz=sc.nextInt();
         System.out.println("整数练习"+"1");
         System.out.println("真分数练习"+"2");
         System.out.println("请选择练习方式:");
         int t=sc.nextInt();
        switch(t)
        {
        case 1:
          
          System.out.println("练习式子为:");ziran(sz,m);break;
        case 2:
            
            System.out.println("练习式子为:");zhenfen(sz,m);
            break;
            
        }
    }
        public static void ziran(int sl,int m){
            String[] x=new String[m+1];
             int a,b,c,j=0;
            int xc=0,fs=0,cs=0,zf2=0,zf3=0;
            boolean flag;
            while(j<m+1)
            {
                
                a=(int)(Math.random()*sl);
                b=(int)(Math.random()*sl+1);
                c=(int)(Math.random()*4);
                zf2=(int)(Math.random()*2);
                zf3=(int)(Math.random()*2);
                x[j]=new String();
                if(c==1&&zf2==0&&zf3==0) x[j]=a+"+"+b+"=";
                if(c==1&&zf2==0&&zf3==1) x[j]=a+"+"+" -"+b+"=";
                if(c==1&&zf2==1&&zf3==0) x[j]="-"+a+"+"+b+"=";
                if(c==1&&zf2==1&&zf3==1) x[j]="-"+a+"+"+"-"+b+"=";
                if(c==2&&zf2==0&&zf3==0) x[j]=a+"-"+b+"=";
                if(c==2&&zf2==0&&zf3==1) x[j]=a+"-"+" -"+b+"=";
                if(c==2&&zf2==1&&zf3==0) x[j]="-"+a+"-"+b+"=";
                if(c==2&&zf2==1&&zf3==1) x[j]="-"+a+"-"+"-"+b+"=";
                 if(c==3&&zf2==0&&zf3==0) x[j]=a+"*"+b+"=";
                 if(c==3&&zf2==0&&zf3==1) x[j]=a+"*"+" -"+b+"=";
                 if(c==3&&zf2==1&&zf3==0) x[j]="-"+a+"*"+b+"=";
                 if(c==3&&zf2==1&&zf3==1) x[j]="-"+a+"*"+"-"+b+"=";
                 if(c==0&&zf2==0&&zf3==0) x[j]=a+"/"+b+"=";
                 if(c==0&&zf2==0&&zf3==1) x[j]=a+"/"+" -"+b+"=";
                 if(c==0&&zf2==1&&zf3==0) x[j]="-"+a+"/"+b+"=";
                 if(c==0&&zf2==1&&zf3==1) x[j]="-"+a+"/"+"-"+b+"=";
                flag=true;
                for(int k=0;k<j;k++)
                {
                    if(x[k].equals(x[j])&&a<b)
                    {
                        flag=false;
                        break;
                    }
                }
                if(flag)
                {
                    if(c!=1&&c!=2) xc++;
                    if((zf2==1&&zf3==1)||(zf2==1&&zf3==0)||(zf2==0&&zf3==1)) fs++;
                    if(c==0&&a%b!=0) cs++;
                    System.out.println(x[j]);
                    j++;
                }
            }
            if(xc>0)
            System.out.println("此次四则之中含有乘除运算,乘除运算有:"+(xc)+"道");
            else System.out.println("此次四则运算不含有乘除运算!");
            if(fs>0)
                System.out.println("此次四则之中含有值为负数的运算值:"+(fs)+"道");
                else System.out.println("此次四则运算不含有值为负数的运算!");
            if(cs>0)
                System.out.println("此次四则之中含有除法有余数的运算式:"+(cs)+"道");
                else System.out.println("此次四则运算不含有除法有余数的运算式!");
            
        }
        public static void zhenfen(int sz,int m){
            String[] y=new String[m+1];
             int h,k,l,f,p;
             int xc1=0,fs1=0,cs1=0,zf,zf1;
             boolean flag1;
             System.out.println(m);
             int i=0;
             while(i<m+1)
             {
                 flag1=true;
                 y[i]=new String();
                 h=(int)(Math.random()*sz);
                 k=(int)(Math.random()*sz+1);
                 f=(int)(Math.random()*sz);
                 p=(int)(Math.random()*sz+1);
                 l=(int)(Math.random()*4);
                 zf=(int)(Math.random()*2);
                 zf1=(int)(Math.random()*2);
                 if(h<k&&f<p)
                 {
                     if(l==1&&zf==0&&zf1==0) y[i]="("+h+"/"+k+")"+" + "+"("+f+"/"+p+")"+"=";
                     if(l==1&&zf==0&&zf1==1) y[i]="("+h+"/"+k+")"+" + "+"("+"-"+f+"/"+p+")"+"=";
                     if(l==1&&zf==1&&zf1==0) y[i]="("+"-"+h+"/"+k+")"+" + "+"("+f+"/"+p+")"+"=";
                     if(l==1&&zf==1&&zf1==1) y[i]="("+"-"+h+"/"+k+")"+" + "+"("+"-"+f+"/"+p+")"+"=";
                     if(l==2&&zf==0&&zf1==0) y[i]="("+h+"/"+k+")"+" - "+"("+f+"/"+p+")"+"=";
                     if(l==2&&zf==0&&zf1==1) y[i]="("+h+"/"+k+")"+" - "+"("+"+"+f+"/"+p+")"+"=";
                     if(l==2&&zf==1&&zf1==0) y[i]="("+"-"+h+"/"+k+")"+" - "+"("+f+"/"+p+")"+"=";
                     if(l==2&&zf==1&&zf1==1) y[i]="("+"-"+h+"/"+k+")"+" - "+"("+"-"+f+"/"+p+")"+"=";
                     if(l==3&&zf==0&&zf1==0) y[i]="("+h+"/"+k+")"+" * "+"("+f+"/"+p+")"+"=";
                     if(l==3&&zf==0&&zf1==1) y[i]="("+h+"/"+k+")"+" * "+"("+"-"+f+"/"+p+")"+"=";
                     if(l==3&&zf==1&&zf1==0) y[i]="("+"-"+h+"/"+k+")"+" * "+"("+f+"/"+p+")"+"=";
                     if(l==3&&zf==1&&zf1==1) y[i]="("+"-"+h+"/"+k+")"+" * "+"("+"-"+f+"/"+p+")"+"=";
                     if(l==0&&zf==0&&zf1==0) y[i]="("+h+"/"+k+")"+" / "+"("+f+"/"+p+")"+"=";
                     if(l==0&&zf==0&&zf1==1) y[i]="("+h+"/"+k+")"+" / "+"("+"-"+f+"/"+p+")"+"=";
                     if(l==0&&zf==1&&zf1==0) y[i]="("+"-"+h+"/"+k+")"+" / "+"("+f+"/"+p+")"+"=";
                     if(l==0&&zf==1&&zf1==1) y[i]="("+"-"+h+"/"+k+")"+" / "+"("+"-"+f+"/"+p+")"+"=";
                     flag1=true;
                     for(int k1=0;k1<i;k1++)
                     {
                         if(y[k1].equals(y[i]))
                         {
                             flag1=false;
                             break;
                         }
                     }
                     if(flag1)
                     {
                         if(l!=1&&l!=2) xc1++;
                         if((zf==1&&zf1==1)||(zf==1&&zf1==0)||(zf==0&&zf1==1)) fs1++;
                         if(l==0&&(h*p)%(f*k)!=0) cs1++;
                         System.out.println(y[i]);
                         i++;
                     }
                 }
                 
             }
             if(xc1>0)
                 System.out.println("此次四则之中含有乘除运算,乘除运算有:"+xc1+"道");
                 else System.out.println("此次四则运算不含有乘除运算!");
             if(fs1>0)
                     System.out.println("此次四则之中含有值为负数的运算值:"+fs1+"道");
                     else System.out.println("此次四则运算不含有值为负数的运算!");
             if(cs1>0)
                     System.out.println("此次四则之中含有除法有余数的运算式:"+cs1+"道");
                     else System.out.println("此次四则运算不含有除法有余数的运算式!");
        }
        
    }

posted @ 2017-03-20 19:50  Janson鑫  阅读(132)  评论(0编辑  收藏  举报