自动生成数学题型二(框架struts2)题型如((a+b)*c=d)

1. 生成题目

1.1 生成单个题目

  1 public static String[] twoOperatorAndOperator(int num1, int num2) {
  2         double first = generate(num1, num2);
  3         double secound = generate(num1, num2);
  4         double third = generate(num1, num2);
  5         System.out.println(num1+" num1");
  6         System.out.println(num2+" num2");
  7         int zero = 0;
  8         zero=generate(1, 28);
  9         System.out.println(zero + "  zero");
 10         switch (zero) {
 11         case 1:
 12             double result = first + secound + third;
 13             String[] addAndAdd = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 14                     String.format("%.0f", third), String.format("%.0f", result), String.valueOf(1), "+", "+", null,
 15                     null, null, null };
 16             return addAndAdd;
 17         case 2:
 18             if (first + secound >= third) {
 19                 double result2 = first + secound - third;
 20                 String[] addAndSub = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 21                         String.format("%.0f", third), String.format("%.0f", result2), String.valueOf(2), "+", "-", null,
 22                         null, null, null };
 23                 return addAndSub;
 24             } else {
 25                 double result2 = third + secound - first;
 26                 String[] addAndSub = new String[] { String.format("%.0f", third), String.format("%.0f", secound),
 27                         String.format("%.0f", first), String.format("%.0f", result2), String.valueOf(2), "+", "-", null,
 28                         null, null, null };
 29                 return addAndSub;
 30             }
 31         case 3:
 32             double result3 = first + secound * third;
 33             String[] addAndMul = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 34                     String.format("%.0f", third), String.format("%.0f", result3), String.valueOf(3), "+", "*", null,
 35                     null, null, null };
 36             return addAndMul;
 37         case 4:
 38             if (third != 0) {
 39                 double result4 = first + secound / third;
 40                 String[] addAndDiv = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 41                         String.format("%.0f", third), String.format("%.2f", result4), String.valueOf(4), "+", "/", null,
 42                         null, null, null };
 43                 return addAndDiv;
 44             } else {
 45                 twoOperatorAndOperator(num1, num2);
 46             }
 47         case 5:
 48             if (first - secound + third >= 0) {
 49                 double result5 = first - secound + third;
 50                 String[] SubAndAdd = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 51                         String.format("%.0f", third), String.format("%.0f", result5), String.valueOf(5), "-", "+", null,
 52                         null, null, null };
 53                 return SubAndAdd;
 54             } else {
 55                 double result5 = secound - first - third;
 56                 String[] SubAndAdd = new String[] { String.format("%.0f", secound), String.format("%.0f", first),
 57                         String.format("%.0f", third), String.format("%.0f", result5), String.valueOf(5), "-", "-", null,
 58                         null, null, null };
 59                 return SubAndAdd;
 60             }
 61         case 6:
 62             if (first - secound - third >= 0) {
 63                 double result6 = first - secound - third;
 64                 String[] SubAndSub = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 65                         String.format("%.0f", third), String.format("%.0f", result6), String.valueOf(6), "-", "-", null,
 66                         null, null, null };
 67                 return SubAndSub;
 68             } else {
 69                 double result6 = third + secound - first;
 70                 String[] SubAndSub = new String[] { String.format("%.0f", third), String.format("%.0f", secound),
 71                         String.format("%.0f", first), String.format("%.0f", result6), String.valueOf(6), "+", "-", null,
 72                         null, null, null };
 73                 return SubAndSub;
 74             }
 75         case 7:
 76             if (first - secound * third > 0) {
 77                 double result7 = first - secound * third;
 78                 String[] SubAndMul = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 79                         String.format("%.0f", third), String.format("%.0f", result7), String.valueOf(7), "-", "*", null,
 80                         null, null, null };
 81                 return SubAndMul;
 82             } else {
 83                 double result7 = third * secound - first;
 84                 String[] SubAndMul = new String[] { String.format("%.0f", third), String.format("%.0f", secound),
 85                         String.format("%.0f", first), String.format("%.0f", result7), String.valueOf(7), "*", "-", null,
 86                         null, null, null };
 87                 return SubAndMul;
 88             }
 89 
 90         case 8:
 91             if (third != 0) {
 92                 if (first - secound / third > 0) {
 93                     double result8 = first - secound / third;
 94                     String[] SubAndDiv = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
 95                             String.format("%.0f", third), String.format("%.2f", result8), String.valueOf(8), "-", "/",
 96                             null, null, null, null };
 97                     return SubAndDiv;
 98                 } else {
 99                     double result8 = secound / third - first;
100                     String[] SubAndDiv = new String[] { String.format("%.0f", secound), String.format("%.0f", third),
101                             String.format("%.0f", first), String.format("%.2f", result8), String.valueOf(8), "/", "-",
102                             null, null, null, null };
103                     return SubAndDiv;
104                 }
105             } else {
106                 twoOperatorAndOperator(num1, num2);
107             }
108         case 9:
109             double result9 = first * secound + third;
110             String[] MulAndAdd = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
111                     String.format("%.0f", third), String.format("%.0f", result9), String.valueOf(9), "*", "+", null,
112                     null, null, null };
113             return MulAndAdd;
114         case 10:
115             if (first * secound - third > 0) {
116                 double result10 = first * secound - third;
117                 String[] MulAndSub = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
118                         String.format("%.0f", third), String.format("%.0f", result10), String.valueOf(10), "*", "-",
119                         null, null, null, null };
120                 return MulAndSub;
121             } else {
122                 double result10 = third - first * secound;
123                 String[] MulAndSub = new String[] { String.format("%.0f", third), String.format("%.0f", first),
124                         String.format("%.0f", secound), String.format("%.0f", result10), String.valueOf(10), "-", "*",
125                         null, null, null, null };
126                 return MulAndSub;
127             }
128 
129         case 11:
130             double result11 = first * secound * third;
131             String[] MulAndMul = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
132                     String.format("%.0f", third), String.format("%.0f", result11), String.valueOf(11), "*", "*", null,
133                     null, null, null };
134             return MulAndMul;
135         case 12:
136             if (third != 0) {
137                 double result12 = first * secound / third;
138                 String[] MulAndDiv = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
139                         String.format("%.0f", third), String.format("%.2f", result12), String.valueOf(12), "*", "/",
140                         null, null, null, null };
141                 return MulAndDiv;
142             } else {
143                 twoOperatorAndOperator(num1, num2);
144             }
145         case 13:
146             if (secound != 0) {
147                 double result13 = first / secound + third;
148                 String[] DivAndAdd = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
149                         String.format("%.0f", third), String.format("%.2f", result13), String.valueOf(13), "/", "+",
150                         null, null, null, null };
151                 return DivAndAdd;
152             } else {
153                 twoOperatorAndOperator(num1, num2);
154             }
155         case 14:
156             if (secound > 0) {
157                 if (first / secound - third > 0) {
158                     double result14 = first / secound - third;
159                     String[] DivAndSub = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
160                             String.format("%.0f", third), String.format("%.2f", result14), String.valueOf(14), "/", "-",
161                             null, null, null, null };
162                     return DivAndSub;
163                 } else {
164                     double result14 = third - first / secound;
165                     String[] DivAndSub = new String[] { String.format("%.0f", third), String.format("%.0f", first),
166                             String.format("%.0f", secound), String.format("%.2f", result14), String.valueOf(14), "-",
167                             "/", null, null, null, null };
168                     return DivAndSub;
169                 }
170             } else {
171                 twoOperatorAndOperator(num1, num2);
172             }
173         case 15:
174             if (secound != 0) {
175                 double result15 = first / secound * third;
176                 String[] DivAndMul = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
177                         String.format("%.0f", third), String.format("%.2f", result15), String.valueOf(15), "/", "*",
178                         null, null, null, null };
179                 return DivAndMul;
180             } else {
181                 twoOperatorAndOperator(num1, num2);
182             }
183         case 16:
184             if (secound != 0 && third != 0) {
185                 double result16 = first / secound / third;
186                 String[] DivAndDiv = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
187                         String.format("%.0f", third), String.format("%.2f", result16), String.valueOf(16), "/", "/",
188                         null, null, null, null };
189                 return DivAndDiv;
190             } else {
191                 twoOperatorAndOperator(num1, num2);
192             }
193         case 17:
194             double result17 = (first + secound) * third;
195             String[] AddAndMul2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
196                     String.format("%.0f", third), String.format("%.0f", result17), String.valueOf(17), "+", "*", "(",
197                     null, ")", null };
198             return AddAndMul2;
199         case 18:
200             if (third != 0) {
201                 double result18 = (first + secound) / third;
202                 String[] AddAndDiv2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
203                         String.format("%.0f", third), String.format("%.2f", result18), String.valueOf(18), "+", "/",
204                         "(", null, ")", null };
205                 return AddAndDiv2;
206             } else {
207                 twoOperatorAndOperator(num1, num2);
208             }
209         case 19:
210             if (first > secound) {
211                 double result19 = (first - secound) * third;
212                 String[] SubAndMul2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
213                         String.format("%.0f", third), String.format("%.0f", result19), String.valueOf(19), "-", "*",
214                         "(", null, ")", null };
215                 return SubAndMul2;
216             } else {
217                 double result19 = (secound - first) * third;
218                 String[] SubAndMul2 = new String[] { String.format("%.0f", secound), String.format("%.0f", first),
219                         String.format("%.0f", third), String.format("%.0f", result19), String.valueOf(19), "-", "*",
220                         "(", null, ")", null };
221                 return SubAndMul2;
222             }
223         case 20:
224             if (third != 0) {
225                 double result20 = (first + secound) / third;
226                 String[] SubAndDiv2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
227                         String.format("%.0f", third), String.format("%.2f", result20), String.valueOf(20), "+", "/",
228                         "(", null, ")", null };
229                 return SubAndDiv2;
230             } else {
231                 twoOperatorAndOperator(num1, num2);
232             }
233         case 21:
234             double result21 = first * (secound + third);
235             String[] MulAndAdd2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
236                     String.format("%.0f", third), String.format("%.0f", result21), String.valueOf(21), "*", "+", null,
237                     "(", null, ")" };
238             return MulAndAdd2;
239         case 22:
240             if (secound > third) {
241                 double result22 = first * (secound - third);
242                 String[] MulAndSub2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
243                         String.format("%.0f", third), String.format("%.2f", result22), String.valueOf(22), "*", "-",
244                         null, "(", null, ")" };
245                 return MulAndSub2;
246             } else {
247                 double result22 = first * (third - secound);
248                 String[] MulAndSub2 = new String[] { String.format("%.0f", first), String.format("%.0f", third),
249                         String.format("%.0f", secound), String.format("%.2f", result22), String.valueOf(22), "*", "-",
250                         null, "(", null, ")" };
251                 return MulAndSub2;
252             }
253         case 23:
254             if ((secound + third) != 0) {
255                 double result23 = first / (secound + third);
256                 String[] DivAndAdd2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
257                         String.format("%.0f", third), String.format("%.2f", result23), String.valueOf(23), "/", "+",
258                         null, "(", null, ")" };
259                 return DivAndAdd2;
260             } else {
261                 twoOperatorAndOperator(num1, num2);
262             }
263         case 24:
264             if (secound > third) {
265                 double result24 = first / (secound - third);
266                 String[] DivAndSub2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
267                         String.format("%.0f", third), String.format("%.2f", result24), String.valueOf(24), "/", "-",
268                         null, "(", null, ")" };
269                 return DivAndSub2;
270             } else if (secound < third) {
271                 double result24 = first / (third - secound);
272                 String[] DivAndSub2 = new String[] { String.format("%.0f", first), String.format("%.0f", third),
273                         String.format("%.0f", secound), String.format("%.2f", result24), String.valueOf(24), "/", "-",
274                         null, "(", null, ")" };
275                 return DivAndSub2;
276             } else {
277                 twoOperatorAndOperator(num1, num2);
278             }
279         case 25:
280             double result25 = first * (secound * third);
281             String[] MulAndMul2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
282                     String.format("%.0f", third), String.format("%.0f", result25), String.valueOf(25), "*", "*", null,
283                     "(", null, ")" };
284             return MulAndMul2;
285 
286         case 26:
287             if (third != 0) {
288                 double result26 = first * (secound / third);
289                 String[] MulAndDiv2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
290                         String.format("%.0f", third), String.format("%.2f", result26), String.valueOf(26), "*", "/",
291                         null,  "(", null, ")" };
292                 return MulAndDiv2;
293             } else {
294                 twoOperatorAndOperator(num1, num2);
295             }
296              
297         case 27:
298             if (secound*third != 0) {
299                 double result27 = first / (secound * third);
300                 String[] DivAndMul2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
301                         String.format("%.0f", third), String.format("%.2f", result27), String.valueOf(27), "/", "*",
302                         null, "(", null, ")" };
303                 return DivAndMul2;
304             } else {
305                 twoOperatorAndOperator(num1, num2);
306             }
307         case 28:
308             if(secound / third!=0&& third!=0){
309                 double result28 = first / (secound / third);
310                 String[] DivAndDiv2 = new String[] { String.format("%.0f", first), String.format("%.0f", secound),
311                         String.format("%.0f", third), String.format("%.2f", result28), String.valueOf(28), "/", "/",
312                         null, "(", null, ")" };
313                 return DivAndDiv2;
314             }else {
315                 twoOperatorAndOperator(num1, num2);
316             }
317         default:
318             break;
319         }
320         return null;
321 
322     }

1.2 生成指定数目和范围的题目

 1 /**
 2      * twoOperator2() 方法,处理参数,并形成新的list集合的对象放入到session中,网页遍历可直接获取
 3      * 
 4      * @param exerciseNumber
 5      *            生成题目数量
 6      * @param num1
 7      *            定义起始范围 num1
 8      * @param num2
 9      *            定义起始范围 num2
10      * @return 返回list集合exerciseTwoOperators
11      */
12 
13     public static List<TwoOperators> twoOperator2(int exerciseNumber, int num1, int num2) {
14 
15         List<TwoOperators> exerciseTwoOperators = new ArrayList<TwoOperators>();
16         TwoOperators twoOperator = null;
17         for (int i = 0; i < exerciseNumber; i++) {
18             String[] twoOperatorAndOperator = twoOperatorAndOperator(num1, num2);
19             try {
20                 twoOperator = new TwoOperators(twoOperatorAndOperator[0], twoOperatorAndOperator[1],
21                         twoOperatorAndOperator[2], twoOperatorAndOperator[3], twoOperatorAndOperator[5],
22                         twoOperatorAndOperator[6], twoOperatorAndOperator[7], twoOperatorAndOperator[8],
23                         twoOperatorAndOperator[9], twoOperatorAndOperator[10]);
24                 exerciseTwoOperators.add(twoOperator);
25             } catch (Exception e) {
26                 e.printStackTrace();
27             }
28         }
29         return exerciseTwoOperators;
30 
31     }

1.3 action类中的代码

 1 public class ThirdGrade {
 2     private int min;
 3     private int max;
 4     private int exerciseNumber;
 5     public int getMin() {
 6         return min;
 7     }
 8     public void setMin(int min) {
 9         this.min = min;
10     }
11     public int getMax() {
12         return max;
13     }
14     public void setMax(int max) {
15         this.max = max;
16     }
17     public int getExerciseNumber() {
18         return exerciseNumber;
19     }
20     public void setExerciseNumber(int exerciseNumber) {
21         this.exerciseNumber = exerciseNumber;
22     }
23     
24     public String execute(){
25         
26         ActionContext actioncontext = ActionContext.getContext();
27         Map<String, Object> session = actioncontext.getSession();
28         count2 cou2=new count2();
29         @SuppressWarnings("static-access")
30         List<TwoOperators> twoOperators=cou2.twoOperator2(exerciseNumber, min, max);
31         System.out.println("twoOperators = "+twoOperators);
32         session.put("twoOperators", twoOperators);
33         return "thirdGradeSuccess";
34     }
35     
36 }

2 .运行结果截图

2.1 初始界面

2.2 用户填写

2.3 回答问题页面

2.4 回答问题

2.5 判断回答结果(判断结果代码在之后)

posted @ 2017-04-09 17:05  九星  阅读(225)  评论(0编辑  收藏  举报