zoukankan      html  css  js  c++  java
  • 软件工程作业01

    题目:生成小学算数题

    设计思想:写一个定制生成数量的函数num(); 写一个生成算术题的函数function();function函数中可以直接生成随机数,用if语句进行分母为0或者减法过后结果为负数的情况的排除,运算符号也用随机数0到3来表示,返回值为String的式子。主函数中用一个String类型的数组保存算数题,用类似查询的方法来查重题目,不重复后输出题目。

    源代码:

    package suanshuti;
    
    import java.util.Scanner;
    
    public class Timu {
    
        static double re[]=new double[1000];
        static int num()
        {
            int num;
            Scanner in =  new Scanner(System.in);
            System.out.print("请输入题目的数量:");
            num = in.nextInt();
            return num;
        }
        static String fun(int r)
        {
            String fu ="";
            String shizi="";
            int a=(int)(Math.random()*(r+1));
            int b=(int)(Math.random()*(r+1));
            int c=(int)(Math.random()*(r+1));
            int d=(int)(Math.random()*(r+1));
            int e=(int)(Math.random()*4);
            if(e==0)
            {
                fu="+";
                while(a>b||c>d||b==0||d==0)
                {
                     a=(int)(Math.random()*(r+1));
                     b=(int)(Math.random()*(r+1));
                     c=(int)(Math.random()*(r+1));
                     d=(int)(Math.random()*(r+1));
                }
                shizi=a+"/"+b+" "+fu+" "+c+"/"+d+" = ?";
            }
            if(e==1)
            {
                fu="-";
                while(a>b||c>d||b==0||d==0)
                {
                     a=(int)(Math.random()*(r+1));
                     b=(int)(Math.random()*(r+1));
                     c=(int)(Math.random()*(r+1));
                     d=(int)(Math.random()*(r+1));
                }
                shizi=a+"/"+b+" "+fu+" "+c+"/"+d+" = ?";
            }
            if(e==2)
            {
                fu="*";
                while(a>b||c>d||b==0||d==0)
                {
                     a=(int)(Math.random()*(r+1));
                     b=(int)(Math.random()*(r+1));
                     c=(int)(Math.random()*(r+1));
                     d=(int)(Math.random()*(r+1));
                }
                shizi=a+"/"+b+" "+fu+" "+c+"/"+d+" = ?";
            }
            if(e==3)
            {
                fu="/";
                while(a>b||c>d||b==0||d==0)
                {
                     a=(int)(Math.random()*(r+1));
                     b=(int)(Math.random()*(r+1));
                     c=(int)(Math.random()*(r+1));
                     d=(int)(Math.random()*(r+1));
                }
                shizi=a+"/"+b+" "+fu+" "+c+"/"+d+" = ?";
            }
            return shizi;
        }
        static String function(int r)
        {
            String fu ="";
            String shizi="";
            int a=(int)(Math.random()*(r+1));
            int b=(int)(Math.random()*(r+1));
            int c=(int)(Math.random()*4);
            if(c==0)
            {
                fu="+";
                shizi=a+" "+fu+" "+b+" = ?";
            }
            if(c==1)
            {
                fu="-";
                //排除负数可能性
                while(a-b<0)
                {
                     a=(int)(Math.random()*(r+1));
                     b=(int)(Math.random()*(r+1));
                }
                shizi=a+" "+fu+" "+b+" = ?";
            }
            if(c==2)
            {
                fu="*";
                shizi=a+" "+fu+" "+b+" = ?";
            }
            if(c==3)
            {
                fu="/";
                //排除分母为0的可能性
                while(b==0)
                {
                    b=(int)(Math.random()*(r+1));
                }    
                shizi=a+" "+fu+" "+b+" = ?";
            }
            return shizi;
        }
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            int num1,r,r1,m=0;
            num1=num();
            String Array[]=new String[num1];
            String Array1[]=new String[num1];
            while(m==0)
            {
            System.out.print("1:真分数 2:数整 3:退出 请输入:");
            Scanner in2 =  new Scanner(System.in);
            r1 = in2.nextInt();
            if(r1==1)
            {
                System.out.print("请输入算数题中出现的数的最大值:");
                Scanner in1 =  new Scanner(System.in);
                r = in1.nextInt();
                for(int i=0;i<num1;i++)
                {
                    Array1[i]=fun(r);
                    //查重
                    for(int z=0;z<i;z++)
                    {
                        if(Array1[z].equals(Array1[i]))
                        {
                            Array1[i]=fun(r);
                            z=0;
                        }
                    }
                    System.out.println(Array1[i]);
                }
            }
            if(r1==2)
            {
            System.out.print("请输入算数题中出现的数的最大值:");
            Scanner in1 =  new Scanner(System.in);
            r = in1.nextInt();
            for(int i=0;i<num1;i++)
            {
                Array[i]=function(r);
                //查重
                for(int z=0;z<i;z++)
                {
                    if(Array[z].equals(Array[i]))
                    {
                        Array[i]=function(r);
                        z=0;
                    }
                }
                System.out.println(Array[i]);
            }
            }
            if(r1==3)
            {
                m=1;
                System.out.print("谢谢使用!");
            }
            }
        }
    }

    结果截图:

  • 相关阅读:
    Windows API的CreateFile()中的OPEN_ALWAYS和CREATE_ALWAYS之间的差异
    Linux下的虚拟串口对(可用于在本机上模拟串口进行调试)
    [转&精]IO_STACK_LOCATION与IRP的一点笔记
    【转】在WIN7、WIN10操作系统用WebDAV映射网络驱动器需要的操作
    Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information
    分布式处理大数据的目录及学习树
    利用docker将传统企业集中式架构转换为微服务架构的实践经验
    docker学习笔记5 端口映射与容器互联
    docker学习笔记4 数据管理、持久化
    docker学习笔记3 容器 container
  • 原文地址:https://www.cnblogs.com/lhj1017/p/6485931.html
Copyright © 2011-2022 走看看