软件工程个人作业01

像二柱子那样,花二十分钟写一个能自动生成三十道小学四则运算题目的 “软件”,

要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。(有能力者改编成网页版)

和同学们比较一下各自程序的功能、实现方法的异同等等。

1.程序设计思想

1.用随机函数,生成两个随机运算数,以及运算符,

2.加一个计时函数

3.用数组存放每个运算式的结果,与做题人输入的答案进行比对,如果对TrueCount加1,如果错误FalseCount加1,最后输出做题数量,以及正确,错误的个数

2.源代码

 1 //信1605-1  寇肖萌  20163446
 2 import java.util.Scanner;
 3 import java.util.Random;
 4 public class size1 {
 5     public static int[] zhengshu(int n)//整数运算
 6     {
 7         long timeStart = System.currentTimeMillis();
 8         int TrueCount=0;
 9         int FalseCount=0;
10            int a[]=new int[n];
11            int b[]=new int [n];
12            int c[]=new int[n];
13            String d[]=new String[n];
14            int result[]=new int[n];
15         for(int i=0;i<n;i++)
16         {
17               a[i]=(int) (Math.random() * 100);
18               c[i]=(int)(Math.random()* 4);
19               b[i]=(int)(Math.random()*100);
20             if(c[i]==1)
21             {
22                 d[i]="+";
23             }
24             else if(c[i]==2)
25             {
26                 d[i]="-";
27             }
28             else if(c[i]==3)
29             {
30                 d[i]="*";
31             }
32             else
33             {
34                 d[i]="/";
35             }
36              //查重
37             int k=i;
38             for(int j=0;j<k;j++)
39             {
40                 if((a[k]==a[j])&&(b[k]==b[j])&&(c[k]==c[j]))
41                     i--;
42                 
43             }
44             if(i==k)
45             {
46                 System.out.println(a[i]+" "+d[i]+" "+b[i]);
47                 Scanner scanner=new Scanner(System.in);
48                 int m=scanner.nextInt();
49                   if(d[i]=="+")
50                       result[i]=a[i]+b[i];
51                      if(d[i]=="-")
52                           result[i]=a[i]-b[i];
53                      if(d[i]=="*")
54                           result[i]=a[i]*b[i];
55                      if(d[i]=="/")
56                           result[i]=a[i]/b[i];
57                      if(m==result[i])
58                      {
59                          TrueCount++;
60                      }
61                      else
62                          FalseCount++;
63                  }
64              
65             }
66         System.out.println("您一共做了"+n+"道题"+" "+"答对了"+TrueCount+"道题"+","+"答错了"+FalseCount+"道题");
67         long timeEnd=System.currentTimeMillis();
68         System.out.println("总共用时:"+(timeEnd-timeStart)+"ms");
69         return result;
70         }
71        
72     
73         
74            
75            
76 
77     
78      public static int SuiJi()//随机运算数
79      {
80             Random a=new Random();
81             return a.nextInt(101);
82      }
83     
84     public static void main(String args[])
85     {
86         System.out.println("请输入要做题的数量:");
87         Scanner scanner=new Scanner(System.in);
88         int num=scanner.nextInt();
89         zhengshu(num);
90     }
91 }

运行结果截图:

原文地址:https://www.cnblogs.com/ggrm/p/7955139.html