四则运算初级版

 

 


 

 

package 课堂测验;

import sun.lwawt.macosx.CSystemTray;

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

 

public class operator1 {

   static int n;

   static int m;

   static int chengchu;

   static int kuohao;

   static int fanwei;

   static int count;

    public static boolean ifSame(int shu[])

    {

        Arrays.sort(shu);

        for (int i = 0; i < shu.length-1; i++)

        {

            if(shu[i]==shu[i+1])

            return false;

        }

        return true;

    }

    public static void canshu()

    {

        Random r = new Random();

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入打印数量");

         n = sc.nextInt();

        System.out.println("请输入打印方式(列数)");

         m = sc.nextInt();

        System.out.println("请选择是否有乘除法(输入1有,输入2没有)");

         chengchu = sc.nextInt();

        System.out.println("请选择是否有括号(输入1有,输入2没有)");

         kuohao=sc.nextInt();

        System.out.println("请输入操作数的范围");

         fanwei = sc.nextInt();

        System.out.println("请输入操作数的个数");

         count = sc.nextInt();

 

    }

    public static void dati()

    {

 

    }

    public static void main(String[] args) {

        canshu();

        Random r=new Random();

        for (int i = 0; i < n; i++)

        {

            int flag=0;

            int czshu[] = new int[count];

 

            Random r1=new Random();

            int zkh=r1.nextInt(count-1);//左括号的位置

            int ykh=r1.nextInt(count)%(count-zkh)+zkh+1;//右括号的位置

            for (int j = 0; j <count-1; j++)

            {

                czshu[j]=r.nextInt(fanwei)+1;

                String fuhao = "";

 

                if(chengchu==1)

                {

                    int f = r.nextInt(4);

                    if (f == 0) {

                        fuhao = "+";

                    } else if (f == 1) {

                        fuhao = "-";

                    } else if (f == 2) {

                        fuhao = "*";

                    } else if (f == 3) {

                        fuhao = "/";

                    }

                }

                else if(chengchu==2)

                {

                    int f = r.nextInt(2);

                    if (f == 0) {

                        fuhao = "+";

                    } else if (f == 1) {

                        fuhao = "-";

                    }

                }

                if(kuohao==1)

                {

                    if (j == zkh)

                    {

                        System.out.print("(");

                    }

                    System.out.print(czshu[j]);

                    if (j == ykh)

                    {

                        System.out.print(")");

                        flag = 1;

                    }

                    System.out.print(fuhao);

                }

                if(kuohao==2)

                {

                    System.out.print(czshu[j]+fuhao);

                }

            }

            int shu[]=new int [n];

            if(kuohao==1)

            {

                if (flag == 0)

                {

                    int s=r.nextInt(fanwei) + 1;

                    System.out.print(s+ ")=");

                    shu[i]=s;

                }

                else

                {

                    int s=r.nextInt(fanwei) + 1;

                    System.out.print(s+ "=");

                    shu[i]=s;

                }

            }

            if(kuohao==2)

            {

                int s=r.nextInt(fanwei) + 1;

                System.out.print(s + "=");

                shu[i]=s;

            }

            Scanner sc=new Scanner(System.in);

            if ((i + 1) % m == 0)

            {

                System.out.print("\n");

            }

            else

            {

                System.out.print("\t\t");

            }

        }

    }

}

大体上都完成了,除了括号其他都不太难,还有就是不会判断题目是否重复 

posted @ 2022-09-17 17:07  Men!  阅读(26)  评论(0编辑  收藏  举报