paperOne基于java web的简易四则运算出题网站

项目成员:张金生     张政

需求概要

1.运算数均为正整数

2.包含的运算符有+,-,*,/

3.除法运算结果为整除运算

4.批量生成题目并判题

核心功能分析

1.题目生成——java后端

  题目生成主要需要对操作数和符号进行生成,操作数由用户选择出题难度,程序划分出不同的难度来使用题目生成器达到对指定难度的题目进行生成。对于题目难度的控制,我们需要知道操作数的范围,需要使用到的符号列表以及括号的引入(括号引入待完善),根据这些参数就可以生成整数运算的不同难度的题目了。生成题目的结果因为包含数字和字符,所以可以使用String来返回。

  主体题目生成函数采取规定范围操作数和选取的符号分别随机生成,最后拼接的方式。其中除法运算相对特殊,为保证结果可以整除,可以使用随机的小因子乘以除数并重新赋值给被除数。除零错误可以在生成随机数时加偏移来避免。

  符号支持列表设计时可以使用枚举类型,这样用户可以选择生成难度的时候可以选择需要的符号,例如一年级只生成加减运算的题目。支持的符号操作可以这样定义

 1 public enum SupportedOperation { ADD, MINUS, MULTIPLY, DIVIDE/*, SQUARE, CUBE, RADICAL*/, ALL; } 

  其中被注释掉的运算符可以版本升级时选择性支持

  题目生成的核心函数的函数头可以这样定义:

 1 public String generateQuestion(int numOfOperand, int rangeMin, int rangMax, boolean isInt,SupportedOperation[] operation, boolean bracket); 

      其中的参数分别代表:操作数数量,范围最小值,范围最大值,是否仅作整数运算并且结果为整数,需要支持的运算符列表,题目是否包含括号

2.对用户提交的答案进行评定——javascript前端

  用户提交的答案评定可使用js进行结果计算并给出,将题目获取后,调用eval函数对获取到的题目表达式计算的结果和用户输入值进行对比,再写入到结果栏,完成评分操作。

部分功能实现

题目生成

 1     public String generateQuestion(int numOfOperand, int rangeMin, int rangMax, boolean isInt,
 2             SupportedOperation[] operation, boolean bracket) {
 3         String question = "";
 4         int[] ioperands = null;
 5         double[] doperands = null;
 6         SupportedOperation[] so = null;
 7         if (numOfOperand < 2) {
 8             System.out.println("操作数数量至少为2");
 9             return "";
10         }
11         if (rangMax > 500) {
12             System.out.println("操作数数最大值不能超过500");
13             return "";
14         }
15         if (isInt) {
16             ioperands = new int[numOfOperand];
17             for (int i = 0; i < numOfOperand; i++) {
18                 ioperands[i] = (int) (Math.random() * rangMax / 2 +1);
19             
20             }
21             question += ioperands[0];
22             //int sub = ioperands[0];
23             so = new SupportedOperation[numOfOperand-1];
24             for(int i = 0;i < operation.length;i++){
25                 if(operation[i] == SupportedOperation.ALL){
26                     operation = new SupportedOperation[4];
27                     operation[0] = SupportedOperation.ADD;
28                     operation[1] = SupportedOperation.MINUS;
29                     operation[2] = SupportedOperation.MULTIPLY;
30                     operation[3] = SupportedOperation.DIVIDE;
31                     
32                 }
33             }
34             int value = 0;
35             for(int j = 0;j<numOfOperand-1;j++){
36                 
37                 so[j] = operation[(int)(Math.random()*operation.length)];
38                 switch(so[j]){
39                 case ADD:question = ioperands[j+1]+"+"+question;break;
40                 case MINUS:question = ioperands[j+1]+"-"+question;break;
41                 case MULTIPLY:question = ioperands[j+1]+"*"+question;break;
42                 case DIVIDE:{
43                     if(value < 1){
44                         ioperands[j+1] = ioperands[j+1]*ioperands[j];
45                         question =ioperands[j+1]+"/"+question;
46                         
47                         value++;
48                     }
49                     else{
50                         j--;
51                     }
52                 }break;
53                 default:System.out.println("sb");break;
54                 }
55             }
56             System.out.println(question);
57             ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");
58             
59             try {
60                 Integer d = (Integer) se.eval(question);
61                 System.out.println(d);
62             } catch (ScriptException e) {
63                 e.printStackTrace();
64             }
65             
66         } else {
67             doperands = new double[numOfOperand];
68             for (int i = 0; i < numOfOperand; i++) {
69                 doperands[i] = Math.random() * rangMax / 2;
70             }
71         }
72 
73         return question;
74 
75     }

答案评定

 1 <script type="text/javascript">
 2         function compute() {
 3 
 4             for (var i = 1; i <= 20; i++) {
 5                 var a = "" + eval(document.getElementById("q" + i).innerHTML);
 6                 var auser = document.getElementById("a" + i).value;
 7                 if (a == auser) {
 8                     document.getElementById("r" + i).innerHTML = "正确";
 9                 } else {
10                     document.getElementById("r" + i).innerHTML = "错误";
11                 }
12             }
13 
14         }
15     </script>

 题目难度设计

 1     public String generateSimpleQuestion() {
 2         SupportedOperation[] operation = { SupportedOperation.ADD, SupportedOperation.MINUS };
 3         return generateQuestion(2, 0, 20, true, operation, false);
 4     }
 5 
 6     public String generateCommonQuestion() {
 7         SupportedOperation[] operation = { SupportedOperation.ADD, SupportedOperation.MINUS,
 8                 SupportedOperation.MULTIPLY, SupportedOperation.DIVIDE };
 9         return generateQuestion(3, 0, 30, true, operation, false);
10     }
11 
12     public String generateMediumQuestion() {
13         SupportedOperation[] operation = { SupportedOperation.ADD, SupportedOperation.MINUS,
14                 SupportedOperation.MULTIPLY, SupportedOperation.DIVIDE };
15         return generateQuestion(4, 0, 50, true, operation, true);
16     }
17 
18     public String generateComplexQuestion() {
19         SupportedOperation[] operation = { SupportedOperation.ALL };
20         return generateQuestion(6, 0, 50, true, operation, true);
21     }

程序运行结果

以简单题目生成20道为例的运行结果:

 

以中等难度的题目生成20道为例的运行结果:

 

可进行的拓展

1.拓展操作数到浮点数

2.括号引入

3.更加丰富的运算符引入

结对编程感受

结对编程可以了解队友的编程习惯和解决问题的思路,学习对方的解决问题方式,而且当一个人遇到瓶颈的时候,另一个人可能会提前想出思路,多种解决方式的时候可以择优选择。这种工作模式有点像cpu的双核处理器,两个线程同时工作,一定程度上可以快速的开发项目,并减少代码合并带来的麻烦。这个工作模式以前也是尝试过的,尤其是主要作为指导在旁边跟进时效率不一定会很高,思维的协调不一定比直接看结果来的快,不过对于学习阶段很是受用。

项目源码地址:https://coding.net/u/jx8zjs/p/paperOne/git

git@git.coding.net:jx8zjs/paperOne.git

原文地址:https://www.cnblogs.com/jx8zjs/p/5843858.html