小学四则运算自动生成程序


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

class Suan1
{
    public int x,y,c;
    public Random random=new Random();
    String m;
    public String shengcheng()
    {
        
         c=random.nextInt(4);
         String fuhao;
         x=random.nextInt(100);
         y=random.nextInt(100);
         if(c==0)
         {
             fuhao="+";
         }
         else if(c==1)
         {
             fuhao="-";
         }
         else if(c==2)
         {
             fuhao="*";
         }
         else
         {
             fuhao="/";
         }
         m=String.valueOf(x)+fuhao+String.valueOf(y);
         return m;
         
    }
    public void shuchu()
    {
        if(c==0)
        {
            System.out.println(x+"+"+y+"=");
        }
        else if(c==1)
        {
            if(x>=y)
            System.out.println(x+"-"+y+"=");
            else
                System.out.println(y+"-"+x+"=");
        }
        else if(c==2)
        {
            if(x*y<100)
            {
                System.out.println(x+"*"+y+"=");
            }
            else
            {
                for(int i=0;;i++)
                {
                    if(x*y<=100)
                    {
                        System.out.println(x+"*"+y+"=");
                        break;
                    }
                    else
                    {
                        y=random.nextInt(100);
                    }
                
                }
            }
        }
        else if(c==3)
        {
            for(int i=0;;i++)
            {
            if(y==0)
            {
                y=random.nextInt();
            }
            else
            {
                break;
            }
            }
                System.out.println(x*y+"/"+y+"=");
                
        }
    }
    public int jianyan(String a[],int s)
    {
       int flag=1;
       for(int i=0;i<s;i++)
       {
           if(m.equals(a[i]))
           {
              System.out.println(m);
              flag=0;
              break;
           }
       }
       return flag;
    }
}
class Suan2
{
    public int m,n,x,y,c;
    public Random random=new Random();
    String s;
    public String shengcheng()
    {
        c=random.nextInt(4);
        x=random.nextInt(50);
        y=random.nextInt(50);
        m=random.nextInt(50);
        n=random.nextInt(50);
        String fuhao;
        if(c==0)
         {
             fuhao="+";
         }
         else if(c==1)
         {
             fuhao="-";
         }
         else if(c==2)
         {
             fuhao="*";
         }
         else
         {
             fuhao="/";
         }
         s=String.valueOf(x)+fuhao+String.valueOf(y);
         return s;
    }
    public void shuchu()
    {
        for(int i=0;;i++)
        {
            if(x==0)
            {
            x=random.nextInt(50);
            }
            else
            {
                break;
            }
        }
        for(int i=0;;i++)
        {
            if(y==0)
            {
            y=random.nextInt(50);
            }
            else
            {
                break;
            }
        }
        for(int i=0;;i++)
        {
            if(m==0)
            {
            m=random.nextInt(50);
            }
            else
            {
                break;
            }
        }
        for(int i=0;;i++)
        {
            if(n==0)
            {
            n=random.nextInt(50);
            }
            else
            {
                break;
            }
        }
        if(c==0)
        {
            if(x>y&&m>n)
            {
            System.out.println(y+"/"+x+"  +  "+n+"/"+m+"=");
            }
            else if(x>y&&m<n)
            {
                System.out.println(y+"/"+x+"  +  "+m+"/"+n+"=");
            }
            else if(x<y&&m>n)
            {
                System.out.println(x+"/"+y+"  +  "+n+"/"+m+"=");
            }
            else
            {
                System.out.println(x+"/"+y+"  +  "+m+"/"+n+"=");
            }
        }
        else if(c==1)
        {
            if(x>y&&m>n)
            {
            System.out.println(y+"/"+x+"  -  "+n+"/"+m+"=");
            }
            else if(x>y&&m<n)
            {
                System.out.println(y+"/"+x+"  -  "+m+"/"+n+"=");
            }
            else if(x<y&&m>n)
            {
                System.out.println(x+"/"+y+"  -  "+n+"/"+m+"=");
            }
            else
            {
                System.out.println(x+"/"+y+"  -  "+m+"/"+n+"=");
            }
        }
        else if(c==2)
        {
            if(x>y&&m>n)
            {
            System.out.println(y+"/"+x+"  *  "+n+"/"+m+"=");
            }
            else if(x>y&&m<n)
            {
                System.out.println(y+"/"+x+"  *  "+m+"/"+n+"=");
            }
            else if(x<y&&m>n)
            {
                System.out.println(x+"/"+y+"  *  "+n+"/"+m+"=");
            }
            else
            {
                System.out.println(x+"/"+y+"  *  "+m+"/"+n+"=");
            }
        }
        else
        {
            if(x>y&&m>n)
            {
            System.out.println(y+"/"+x+"  /  "+n+"/"+m+"=");
            }
            else if(x>y&&m<n)
            {
                System.out.println(y+"/"+x+"  /  "+m+"/"+n+"=");
            }
            else if(x<y&&m>n)
            {
                System.out.println(x+"/"+y+"  /  "+n+"/"+m+"=");
            }
            else
            {
                System.out.println(x+"/"+y+"  /  "+m+"/"+n+"=");
            }
        }
    }
    public int jianyan(String a[],int l)
    {
         int flag=1;
           for(int i=0;i<l;i++)
           {
               if(s.equals(a[i]))
               {
                  System.out.println(m);
                  flag=0;
                  break;
               }
           }
           return flag;
    }
}
public class Jisuan {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Random random=new Random();
        Suan1 a=new Suan1();
        Suan2 b=new Suan2();
        Scanner in=new Scanner(System.in);
        String shuju[]=new String[30];
        int flag=0;
        int m=0;
        for(int i=0;i<30;i++)
        {
            shuju[i]=new String();
        }
        
        for(int i=0;;i++)
        {
          System.out.println("1   整数的四则计算");
          System.out.println("2   真分数的四则计算");
          System.out.println("3   整数与真分数的混合四则运算");
          System.out.println("4   退出");
          System.out.println("请选择:");
          int t=in.nextInt();
          if(t==1)
          {
            while(m<30)
             {
                    shuju[m]=a.shengcheng();
                    flag=a.jianyan(shuju, m);
                    if(flag==1)
                    {
                        a.shuchu();
                        m++;
                        flag=0;
                    }
             }
            m=0;
          }
          else if(t==2)
          {
              while(m<30)
                {
                  shuju[m]=b.shengcheng();
                  flag=b.jianyan(shuju, m);
                    if(flag==1)
                    {
                        b.shuchu();
                        m++;
                        flag=0;
                    }
                }
              m=0;
          }
          else if(t==3)
          {
              while(m<30)
                {
                  int suiji=random.nextInt(2);
                  if(suiji==0)
                  {
                  shuju[m]=b.shengcheng();
                  flag=b.jianyan(shuju, m);
                    if(flag==1)
                    {
                        b.shuchu();
                        m++;
                        flag=0;
                    }
                  }
                  else
                  {
                      shuju[m]=a.shengcheng();
                        flag=a.jianyan(shuju, m);
                        if(flag==1)
                        {
                            a.shuchu();
                            m++;
                            flag=0;
                        }
                  }
                }
              m=0;
          }
          else
          {
              break;
          }
        }
        

    }

}

 
















 

posted @ 2017-03-02 18:41  大神笨蛋  阅读(379)  评论(0编辑  收藏  举报