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

    软件工程作业01

    写一个能自动生成三十道小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。

    思路:

    1、通过产生随机数生成百以内的int类型数据,包装方法,在方法中通过随机的方法确定这个数据是否为分数,如果为分数,则产生两个随机数相除,为保证为真分数,在生成算式之前进行判断,如果分母小于分子,则分母等于分子+分母。

    2、判断是否重复:定义一个String类型数组,将生成的算式保存,每次输出之前先验证是否存在,如果存在则不输出,不计数。

      1 package demo;
      2 
      3 import java.util.*;
      4 
      5 public class Random {
      6     @SuppressWarnings("resource")
      7     public static void main(String args[])
      8     {
      9         Scanner sc = new Scanner(System.in);
     10         int num;
     11         System.out.println("请输入需要生成的个数。");
     12         num = sc.nextInt();
     13         String[] s = new String[100];//保存已有的运算式
     14         int ss = 0;//运算式个数计数
     15         char[] ch = new char[4];
     16         ch[0] = '+';
     17         ch[1] = '-';
     18         ch[2] = '*';
     19         ch[3] = '/';
     20         int a,b,a1,b1;
     21         double A,B,A1,B1;
     22         int k;
     23         String str;
     24         int flagA = 0,flagB = 0;//判断是否用真分数。
     25         for(int i = 0;i < num;i ++)
     26         {
     27             flagA = SuiJileixing();
     28             flagB = SuiJileixing();
     29             if(flagA == 1 && flagB == 1)
     30             {
     31                 a = SuiJi1();
     32                 b = SuiJi1();
     33                 k = SuiJiyunsuanfu();
     34                 str = a + " " + ch[k] + " " + b + " = ";
     35                 if(Check(s,str,ss) != 1)
     36                 {
     37                     s[ss] = str;
     38                     System.out.print(s[ss]);
     39                     A = (double)a;
     40                     B = (double)b;
     41                     show(A,B,k);
     42                     ss ++;
     43                 }
     44             }
     45             else if(flagA == 1 && flagB == 0)
     46             {
     47                 a = SuiJi1();
     48                 b = SuiJi1();
     49                 b1 = SuiJi1() + 1;
     50                 if(b < b1)
     51                 {
     52                     b = b;
     53                 }
     54                 else 
     55                 {
     56                     b1 = b1 + b;
     57                 }
     58                 k = SuiJiyunsuanfu();
     59                 str = a + " " + ch[k] + " (" + b + "/" + b1 + ") = ";
     60                 if(Check(s,str,ss) != 1)
     61                 {
     62                     s[ss] = str;
     63                     System.out.print(s[ss]);
     64                     A = (double)a;
     65                     B = (double)b;
     66                     B1 = (double)b1;
     67                     show(A,B/B1,k);
     68                     ss ++;
     69                 }
     70             }
     71             else if(flagA == 0 && flagB == 1)
     72             {
     73                 a = SuiJi1();
     74                 a1 = SuiJi1() + 1;
     75                 b = SuiJi1();
     76                 if(a > a1)
     77                 {
     78                     a1 = a + a1;
     79                 }
     80                 k = SuiJiyunsuanfu();
     81                 str = "(" + a + "/" + a1 + ") " + ch[k] + " " + b + " = ";
     82                 if(Check(s,str,ss) != 1)
     83                 {
     84                     s[ss] = str;
     85                     System.out.print(s[ss]);
     86                     A = (double)a;
     87                     A1 = (double)a1;
     88                     B = (double)b;
     89                     show(A/A1,B,k);
     90                     ss ++;
     91                 }
     92             }
     93             else
     94             {
     95                 a = SuiJi1();
     96                 a1 = SuiJi1() + 1;
     97                 b = SuiJi1();
     98                 b1 = SuiJi1() + 1;
     99                 if(a > a1)
    100                 {
    101                     a1 = a + a1;
    102                 }
    103                 if(b > b1)
    104                 {
    105                     b1 = b + b1;
    106                 }
    107                 k = SuiJiyunsuanfu();
    108                 str =  "(" + a + "/" + a1 + ") " + ch[k] + " " + b + "/" + b1 + " = ";
    109                 if(Check(s,str,ss) != 1)
    110                 {
    111                     s[ss] = str;
    112                     System.out.print(s[ss]);
    113                     A = (double)a;
    114                     A1 = (double)a1;
    115                     B = (double)b;
    116                     B1 = (double)b1;
    117                     show(a/a1,b/b1,k);
    118                     ss ++;
    119                 }
    120             }
    121 
    122             
    123             
    124         }
    125         
    126     }
    127     
    128     public static int SuiJi1()
    129     {
    130         int a;
    131         a = (int)(Math.random()*100);
    132         return a;
    133     }
    134     public static int SuiJiyunsuanfu()
    135     {
    136         int a;
    137         a = (int) (Math.random()*4);
    138         return a;
    139     }
    140     public static int Check(String s[],String str,int ss)
    141     {
    142         int qqq = 0;
    143         for(int i = 0;i < ss;i ++)
    144         {
    145             if(str.equals(s[ss]))
    146             {
    147                 qqq = 1;
    148                 break;
    149             }
    150         }
    151         return qqq;
    152     }
    153     public static int SuiJileixing()
    154     {
    155         int flag = (int)(Math.random()*2);
    156         return flag;
    157     }
    158     public static void show(double a,double b,int k)
    159     {
    160         if(k == 0)
    161         {
    162             System.out.println(a + b);
    163         }
    164         if(k == 1)
    165         {
    166             System.out.println(a - b);
    167         }
    168         if(k == 2)
    169         {
    170             System.out.println(a * b);
    171         }
    172         if(k == 3)
    173         {
    174             System.out.println(a / b);
    175         }
    176     }
    177 }

  • 相关阅读:
    第二十节:Scrapy爬虫框架之使用Pipeline存储
    第十九节:Scrapy爬虫框架之Middleware文件详解
    第十八节:Scrapy爬虫框架之settings文件详解
    第十七节:Scrapy爬虫框架之item.py文件以及spider中使用item
    第十六节:Scrapy爬虫框架之项目创建spider文件数据爬取
    第一节:python提取PDF文档中的图片
    第十五节:Web爬虫之selenium动态渲染爬取
    H5移动开发底部导航-博客园老牛大讲堂
    ajax跨域通信-博客园老牛大讲堂
    博客园模板2--博客园老牛大讲堂
  • 原文地址:https://www.cnblogs.com/jiandanqinxin/p/6504169.html
Copyright © 2011-2022 走看看