软件工程个人作业1

四则运算的设计思路:

1、定义三个整型变量分别表示两个数组,代表运算符;

2、设置一个长度为30的循环,保证其输出30道题;

3、在刚才设置循环外层设置另一层循环,使得程序一次运行可重复使用。

4、除法会判断分母是否为0,如果为0,则多循环一次来保证出题数目为30,如果不为0,则按原来循环进行。

 5、对真分数的判断,如果分子大于分母则将两者位置互换

6、在对真分数进行除法运算时会对第二个真分数的分子和分母进行是否为0的判断。

程序源码:

  1 package 四则运算;
  2 import java.util.Random;
  3 
  4 import javax.swing.JOptionPane;
  5 
  6 public class 运算 {
  7 
  8     public static void main(String[] args) {
  9         // TODO 自动生成的方法存根
 10         int m=1;
 11        int a=0,b=0,c=0,d=0,e=0;
 12        int x=0;
 13        String p="",q="";
 14        Random random = new Random();
 15        
 16        for(int w=0;w<1000000000;w++)
 17        {
 18            String input=JOptionPane.showInputDialog("请输入选择:1、整数 2、真分数  3、退出程序");
 19            x=Integer.parseInt(input);
 20            if(x==1)
 21           {
 22             for(int i=0;i<30;i++)
 23            {
 24                  a = random.nextInt(99);
 25                b=random.nextInt(99);
 26                c=random.nextInt(3);
 27                if(c==0)
 28                {
 29                    System.out.println(a+"+"+b+"=");
 30                }
 31                if(c==1)
 32               {
 33                    System.out.println(a+"-"+b+"=");
 34               }
 35               if(c==2)
 36               {
 37                   System.out.println(a+"*"+b+"=");
 38               }
 39              if(c==3)
 40              {
 41                 if(b!=0)
 42                 {
 43                    System.out.println(a+"/"+b+"=");
 44                 }
 45                 if(b==0)
 46                 {
 47                     i--;
 48                 }
 49              }
 50             
 51            }
 52          }
 53         if(x==2)
 54         {
 55             for(int r=0;r<30;r++)
 56             {
 57                 a = random.nextInt(99);
 58                 b=random.nextInt(99);
 59                 d = random.nextInt(99);
 60                 e=random.nextInt(99);
 61                 c=random.nextInt(3);
 62               if((a>=b&&b!=0)||a==0)
 63              {
 64                 p=b+"/"+a;
 65              }
 66              else 
 67              {
 68                  p=a+"/"+b;
 69              }
 70              if((d>=e&&e!=0)||d==0)
 71              {
 72                  q=e+"/"+d;
 73              }
 74              else 
 75              {
 76                  q=d+"/"+e;
 77              }
 78              if(c==0)
 79              {
 80                  System.out.println(p+"+"+q);
 81              }
 82              if(c==1)
 83              {
 84                  System.out.println(p+"-"+q);
 85              }
 86              if(c==2)
 87              {
 88                  System.out.println(p+"*"+q);
 89              }
 90              if(c==3)
 91              {
 92                  if(d==0||e==0)
 93                  {
 94                      r--;
 95                  }
 96                  else
 97                  {
 98                      System.out.println(p+"/"+q);
 99                  }
100              }
101             }
102         }
103         if(x==3)
104          {break;}
105        }
106     }
107 
108 }

 

  

 

运行结果:

 

 

未及时完成的原因:

对java程序的应用掌握不是很熟练,还需多加练习,对程序的设计思路不能很清晰快速的整理出来。

posted @ 2016-03-07 16:33  夕颜mu  阅读(247)  评论(2编辑  收藏  举报