需求概要
1.运算数均为正整数
2.包含的运算符有+,-,*,/
3.除法运算结果为整除运算
4.批量生成题目并判题
核心功能分析
1.题目生成——java后端
<待编辑>
2.对用户提交的答案进行评定——javascript前端
<待编辑>
部分功能实现
题目生成
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.拓展操作数到浮点数
2.括号引入
项目源码地址:https://coding.net/u/jx8zjs/p/paperOne/git
时间: 2024-11-13 00:56:22