自动生成小学四则运算3

1、可定制打印数量

2、可定制操作数个数

3、可定制是否有乘除法

4、可定制是否有括号

5、可定制操作数取值范围

import java.util.Random;
import java.util.Scanner;
public class Math03 {
    public static String operator(int t)
    {
        String c=null;
        switch(t)
        {
            case 0:c="+";break;
            case 1:c="-";break;
            case 2:c="*";break;
            case 3:c="/";break;
        }
        return c;
    }
    public static int ran(int t)
    {
        Random r=new Random();
        int e;
        e=r.nextInt(t);
        return e;
    }
    public static void main(String[] args) {
        
        Scanner in=new Scanner(System.in);
        Random r=new Random();
        int n,c,t,x,k,j;
        System.out.println("输入题个数:");
        n=in.nextInt(); //n为题数
        System.out.println("输入操作数个数:");
        c=in.nextInt();  //c为操作数
        System.out.println("是否有乘除:(0无乘除,1有乘除)");
        x=in.nextInt();   //x判断是否有乘除
        System.out.println("是否有括号:(0无,1有)");
        k=in.nextInt();    //k判断是否有括号
        System.out.println("输入取值范围:");
        t=in.nextInt();  //n为取值范围
        if(x==0)
        {
            for(int i=0;i<n;i++)
            {
                if(k==0)
                {
                    if(c==2)System.out.println(ran(t)+operator(ran(1))+ran(t)+"=");
                    else if(c==3)System.out.println(ran(t)+operator(ran(1))+ran(t)+operator(ran(t))+ran(t)+"=");
                    else if(c==4)System.out.println(ran(t)+operator(ran(t))+ran(t)+operator(ran(1))+ran(t)+operator(ran(1))+ran(t)+"=");
                }
            else 
            {
                if(c==2)System.out.println(ran(t)+operator(ran(1))+ran(t)+"=");
                else if(c==3)System.out.println("("+ran(t)+operator(ran(1))+ran(t)+")"+operator(ran(1))+ran(t)+"=");
                else if(c==4)System.out.println(ran(t)+operator(ran(1))+"("+ran(t)+operator(ran(1))+ran(t)+")"+operator(ran(1))+ran(t)+"=");
            }
            }
        }    
        else 
        {
                for(int i=0;i<n;i++)
                {
                    if(k==0)
                    {
                        if(c==2)System.out.println(ran(t)+operator(ran(3))+ran(t)+"=");
                        else if(c==3)
                        {
                            switch(ran(1))
                            {
                            case 0:System.out.println(ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+"=");
                            case 1:System.out.println(ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+"=");
                            }
                        }
                        else if(c==4)
                        {
                            switch(ran(2))
                            {
                            case 0:System.out.println(ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+"=");
                            case 1:System.out.println(ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+"=");
                            case 2:System.out.println(ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+"=");
                            }
                        }
                    }
                    else
                    {
                    if(c==2)System.out.println(ran(t)+operator(ran(3))+ran(t)+"=");
                    else if(c==3)
                    {
                        switch(ran(1))
                        {
                        case 0:System.out.println("("+ran(t)+operator(ran(3))+ran(t)+")"+operator(ran(3))+ran(t)+"=");
                        case 1:System.out.println(ran(t)+operator(ran(3))+"("+ran(t)+operator(ran(3))+ran(t)+")"+"=");
                        }
                    }
                    else if(c==4)
                    {
                        switch(ran(2))
                        {
                        case 0:System.out.println("("+ran(t)+operator(ran(3))+ran(t)+")"+operator(ran(3))+ran(t)+operator(ran(3))+ran(t)+"=");
                        case 1:System.out.println(ran(t)+operator(ran(3))+"("+ran(t)+operator(ran(3))+ran(t)+")"+operator(ran(3))+ran(t)+"=");
                        case 2:System.out.println(ran(t)+operator(ran(3))+ran(t)+operator(ran(3))+"("+ran(t)+operator(ran(3))+ran(t)+")"+"=");
                        }
                    }
                }
                
            }
            
        }    
    }
}

 

posted @ 2021-09-22 21:36  Blue啊  阅读(31)  评论(0编辑  收藏  举报