软件工程个人作业01

1.设计思想

        运算符号使用0-3的随机数,来决定‘+’‘-’‘*’‘/’;然后定义四个变量随机产生0-99的整数;在另一个变量产生随机数的情况下,等于0时,整数进行四则运算;等于1时,分数进行四则运算。再使用循环,以完成三十个运算的随机产生。

2.源程序代码

public class test1 {
    public static void main(String[] args)
    {
        int k;
        for(k=0;k<30;k++)
        {
            int a,b,c,d,h;
            //产生0-3的随机数,决定运算符号
            int i=(int)(Math.random()*4);
            //产生四个0-99的随机数,进行运算
            a=(int) (Math.random()*100);
            b=(int) (Math.random()*100);
            c=(int) (Math.random()*100);
            d=(int) (Math.random()*100);
            h=(int) (Math.random()*2);
            //产生0-1的随机数决定是分数运算还是整数运算
            if(h==0)
            {
                //分母不能等于0
                if(b==0||d==0)
                {
                       b=(int) (Math.random()*99+1);
                       d=(int) (Math.random()*99+1);
                }
                if(i==0)
                {
                    System.out.println(a+"/"+b+" + "+c+"/"+d+"=");
                }
                else if(i==1)
                {
                    System.out.println(a+"/"+b+" - "+c+"/"+d+"=");
                }
                else if(i==2)
                {
                   System.out.println(a+"/"+b+" * "+c+"/"+d+"=");
                }
                else
                { 
                   System.out.println(a+"/"+b+" / "+c+"/"+d+"=");
                }
            }
            else 
            {
                if(i==0)
                {
                    System.out.println(a+"+"+b+"=");
                }
                else if(i==1)
                {
                    System.out.println(a+"-"+b+"=");
                }
                else if(i==2)
                {
                   System.out.println(a+"*"+b+"=");
                }
                else
                { 
                   if(b==0)
                       b=(int) (Math.random()*99+1);
                   System.out.println(a+"/"+b+"=");
                }
            }
        }
    }
}

3.运行结果截图

4.未按时完成的原因

        分数、整数的随机产生有些混乱,只写了大概。

原文地址:https://www.cnblogs.com/yyting/p/5251898.html