2021/09/23 第三阶段

fun类

import java.util.Random;
public class fun {
    public int len1;//打印个数
    public int len2;//操作数个数
    public int multi;//乘除法
    public int paren;//括号
    public int leftR;//最小值
    public int rightR;//最大值
    public int leftP;//左括号
    public int rightP;//右括号
    public int index;//记录重复生成次数是否过多

    public fun() {
        this.len1 = 0;
        this.len2 = 0;
        this.multi = 0;
        this.paren = 0;
        this.leftR = 0;
        this.rightR = 0;
        this.index = 0;
    }

    public void f(int i1, int i2, int i3, int i4, int i5, int i6) {

        this.len1 = i1;
        this.len2 = i2;
        this.multi = i3;
        this.paren = i4;
        this.leftR = i5;
        this.rightR = i6;
        //System.out.println("------"+i1);
        //System.out.println("------"+i2);

        if (this.multi == 1) {
            this.multi = 4;
        } else {
            this.multi = 2;
        }

        Random random = new Random();


        int[] a = new int[len1];//存储操作数
        int[] b = new int[len2];//存储操作符
        String[] jiLu = new String[len1];//记录出现过的题目

        int i = 0;
        if (this.paren == 1) {
            this.paren = len1;
        }

        while (i < len1) {

            jiLu[i] = "";
            String str = "";

            for (int j = 0; j < len2; j++) {//生成操作数
                //System.out.println(j);
                a[j] = random.nextInt(this.rightR - this.leftR + 1) + this.leftR;
                //101+200=200-300
                jiLu[i]+=a[j];
            }

            /*System.out.println(a[0]);
            System.out.println("------------"+a[0]);
            String str = String.valueOf(a);
            System.out.println("--------"+str);
            jiLu[i] += str;*/

            for (int j = 0; j < len2 - 1; j++) {//生成操作符
                b[j] = random.nextInt(this.multi);
                jiLu[i]+=b[j];
            }

            /*str = String.valueOf(b);
            jiLu[i] += str;
            jiLu[i] += str;*/

            if (this.paren != 0) {
                //0+0+0=
                this.leftP = random.nextInt(len2 - 1);//左括号能存在的位置
                jiLu[i] += this.leftP;
                //System.out.print("------"+this.leftP);
                //0,1,2,3,4
                this.rightP = random.nextInt(len2 - this.leftP - 1) + this.leftP + 1;//右括号位置
                //System.out.println("------"+this.leftP);
                jiLu[i]+=this.rightP;
            }

            /*str = String.valueOf(this.leftP);
            jiLu[i] += str;
            str = String.valueOf(this.rightP);
            jiLu[i] += str;*/

            //System.out.println("----------" + jiLu[i]);
            //System.out.println("-------2");
            int flag = 0;
            for (int j = 0; j < i && flag == 0; j++) {
                //System.out.println("-------1");
                //System.out.println(jiLu[j]);
                if (jiLu[j].equals(jiLu[i])) {
                    flag = 1;
                    this.index++;
                }
            }

            //System.out.println("-------3");
            if (this.index >= len1 * len1) {
                i = len1;
                System.out.println("重复生成次数过多,程序自动结束,请检查操作数个数与生成题目之间是否合法");
            }
            if (flag == 0) {
                int iFlag = 0;
                for (int j = 0; j < len2; j++) {

                    if (this.paren >= 1 && this.leftP == j) {
                        System.out.print("(");
                    }
                    System.out.print(a[j]);
                    if (this.paren >= 1 && this.rightP == j) {
                        System.out.print(")");
                        this.paren--;
                    }
                    if (iFlag != len2 - 1) {
                        this.f2(b[j]);
                    }
                    iFlag++;

                }

                System.out.println("=");

                //System.out.println(i);

                i++;
            }

        }
    }

    public void f2(int i) {
        if (i == 0) {
            System.out.print("+");
        } else if (i == 1) {
            System.out.print("-");
        } else if (i == 2) {
            System.out.print("*");
        } else if (i == 3) {
            System.out.print("/");
        }
    }
}

  主类

import java.util.Scanner;

public class a {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入需要生成的题数");
        int choice1 = sc.nextInt();
        //System.out.println(len);
        System.out.println("请输入需要生成的操作数");
        int choice2 = sc.nextInt();
        //System.out.println(len2);
        System.out.println("请输入是否需要加入乘除法,1表示需要,0表示不需要");
        int choice3=sc.nextInt();
        System.out.println("请输入是否需要括号,1表示需要,0表示不需要");
        int choice4=sc.nextInt();
        System.out.println("请输入操作数的最小范围");
        int choice5=sc.nextInt();
        System.out.println("请输入操作数的最大范围");
        int choice6=sc.nextInt();


        new fun().f(choice1,choice2,choice3,choice4,choice5,choice6);

    }
}

  

posted @ 2021-09-23 20:17  lzzs111  阅读(35)  评论(0)    收藏  举报