1. 加减乘除
1.1 随机生成制定范围的整数
1 /** 2 * 随机产生一个被限定范围的整数 3 * 4 * @param num1 5 * 定义起始范围 num1 6 * @param num2 7 * 定义终止范围 num2 8 * @return 返回一个 num1 到num2 之间的随机整数数值,且num1<num2 9 * 10 */ 11 12 public static int generate(int num1, int num2) { 13 boolean i = num1 <= num2; 14 int k = -1; 15 // 如果i为true,即num1<num2,返回num1到num2之间的值,否则返回-1; 16 if (i == true) 17 k = (int) (Math.random() * (num2 - num1 + 1)) + num1; 18 return k; 19 20 }
1.2 加法运算
1 /** 2 * 加法运算,生成加数、被加数和和组成的数组 3 * 4 * @param num1 5 * 定义起始范围 num1 6 * @param num2 7 * 定义终止范围 num2 8 * @return 返回数组add,包括加数,被加数,和随机生成的第一个数与第二个整数的和, 此两个整数在num1 和num2之间 9 */ 10 11 public static double[] addition(int num1, int num2) { 12 int first = generate(num1, num2);// 随机生成的第一个整数(加数) 13 int secound = generate(num1, num2);// 随机生成的第二个整数(被家数) 14 int result = first + secound;// 第一个整数与第二个整数相加得到的整数 15 double[] add = new double[] { first, secound, result, 0, 0 }; 16 return add; 17 }
1.3 减法运算
1 /** 2 * 减法运算,生成减数、被减数和差组成的数组 3 * 4 * @param num1 5 * 定义起始范围 num1 6 * @param num2 7 * 定义终止范围 num2 8 * @return 返回数组sub[],包括减数,被减数,和第二个随机生成的数值减去第一个随机生成的数值的差 9 * (第一个生成数值大于第二个生成数值,此两个整数在num1 和num2之间) 10 * 11 */ 12 public static double[] subtraction(int num1, int num2) { 13 int first = generate(num1, num2);// 随机生成的第一个数(减数) 14 int secound = generate(first, num2);// 随机生成的第二个数(被减数) 15 int result = secound - first;// 第二个随机生成的数值减去第一个随机生成的数值的值 16 double[] sub = new double[] { secound, first, result, 0, 1 }; 17 return sub; 18 }
1.4 乘法运算
1 /** 2 * 乘法运算,生成乘数、被乘数和积组成的数组 3 * 4 * @param num1 5 * 定义起始范围 num1 6 * @param num2 7 * 定义终止范围 num2 8 * @return 返回数组,包括乘数,被乘数和随机生成的数的积此(此两个整数在num1 和num2之间 9 */ 10 11 public static double[] multiplication(int num1, int num2) { 12 int first = generate(num1, num2);// 随机生成的第一个整数(乘数) 13 int secound = generate(num1, num2);// 随机生成的第二个整数(被乘数) 14 int result = first * secound;// 第一个整数与第二个整数相加得到的整数 15 double[] mult = new double[] { first, secound, result, 0, 3 }; 16 return mult; 17 18 }
1.5 除法运算
1 /** 2 * 除法运算,返回除数、被除数和商数组成的数组 3 * 4 * @param num1 5 * 定义起始范围 num1 6 * @param num2 7 * 定义终止范围 num2 8 * @return 9 */ 10 11 public static double[] division2(int num1, int num2) { 12 double first = generate(num1, num2);// 随机生成的第一个整数(除数) 13 double secound = generate((int)first, num2);// 随机生成的第二个整数(被除数) 14 double result = secound / first; 15 double[] div2 = new double[] { secound, first, result, 0, 4 }; 16 return div2; 17 }
1.6 求余运算
1 /** 2 * 除法求余数运算,生成除数、被除数、积和余数组成的数组 3 * 4 * @param num1 5 * 定义起始范围 num1 6 * @param num2 7 * 定义终止范围 num2 8 * @return 返回数组div,包阔除数,被除数,余数和随机生成的数两个数的商(随机生成的数第一个大于第二个数) 9 */ 10 public static double[] division(int num1, int num2) { 11 12 int first = generate(num1, num2);// 随机生成的第一个整数(除数) 13 int secound = generate(first, num2);// 随机生成的第二个整数(被除数) 14 int result = secound / first; 15 int remainder = secound % first; 16 double[] div = new double[] { secound, first, result, remainder, 5 }; 17 return div; 18 }
1.7 加减乘除四种随机生成
/** * 随机生成加、减、乘、除四种的数组 * * @param num5 * 定义起始范围 num5 * @param num6 * 定义终止范围 num6 * @return 随机生成的一组数组 */ public static double[] fourExercise(int num5, int num6) { final int num1 = 0; final int num2 = 1; final int num3 = 2; final int num4 = 3; int num = generate(num1, num4); switch (num) { case num1: double[] sub = subtraction(num5, num6); return sub; case num2: double[] add = addition(num5, num6); return add; case num3: double[] mult = multiplication(num5, num6); return mult; case num4: double[] div = division2(num5, num6); return div; default: break; } return null; }
2.判断处理
2.1 根据题目类型和数量生成题目
1 /** 2 * judge方法,处理参数,并形成新的list集合的对象 3 * 4 * @param exerciseNumber 5 * 习题数量 6 * @param exerciseType 7 * 习题种类(加减乘除) 8 * @param num5 9 * 起始数值 10 * @param num6 11 * 结束数值 12 * @return 返回list集合 13 */ 14 15 public List<Number3> judge(int exerciseNumber, int exerciseType, int num5, int num6) { 16 final int num1 = 0; 17 final int num2 = 1; 18 final int num3 = 2; 19 final int num4 = 3; 20 final int num7 = 4; 21 final int num8 = 5; 22 List<Number3> exercise = new ArrayList<Number3>(); 23 Number3 number = null; 24 switch (exerciseType) { 25 case num1: 26 for (int i = 0; i < exerciseNumber; i++) { 27 double[] sub = subtraction(num5, num6); 28 number = new Number3(); 29 number.setNum1((int) sub[0]); 30 number.setNum2((int) sub[1]); 31 number.setNum3(String.format("%.0f", sub[2])); 32 number.setNumType("-"); 33 number.setSymbol("="); 34 exercise.add(number); 35 } 36 return exercise; 37 case num2: 38 for (int i = 0; i < exerciseNumber; i++) { 39 double[] add = addition(num5, num6); 40 number = new Number3(); 41 number.setNum1((int) add[0]); 42 number.setNum2((int) add[1]); 43 number.setNum3(String.format("%.0f", add[2])); 44 number.setNumType("+"); 45 number.setSymbol("="); 46 exercise.add(number); 47 } 48 return exercise; 49 case num3: 50 for (int i = 0; i < exerciseNumber; i++) { 51 double[] mult = multiplication(num5, num6); 52 number = new Number3(); 53 number.setNum1((int) mult[0]); 54 number.setNum2((int) mult[1]); 55 number.setNum3(String.format("%.0f", mult[2])); 56 number.setNumType("*"); 57 number.setSymbol("="); 58 exercise.add(number); 59 System.out.println(exercise); 60 } 61 return exercise; 62 case num4: 63 for (int i = 0; i < exerciseNumber; i++) { 64 double[] div = division(num5, num6); 65 number = new Number3(); 66 number.setNum1((int) div[0]); 67 number.setNum2((int) div[1]); 68 number.setNum3(String.format("%.0f", div[2])); 69 number.setNum4(String.format("%.0f", div[3])); 70 number.setNumType("/"); 71 number.setSymbol("="); 72 exercise.add(number); 73 } 74 return exercise; 75 case num7: 76 for (int i = 0; i < exerciseNumber; i++) { 77 double[] fourexercise = fourExercise(num5, num6); 78 number = new Number3(); 79 System.out.println(exerciseType + "exerciseType"); 80 if (fourexercise[4] == 0) { 81 number.setNum1((int) fourexercise[0]); 82 number.setNum2((int) fourexercise[1]); 83 number.setNum3(String.format("%.0f", fourexercise[2])); 84 number.setNumType("-"); 85 number.setSymbol("="); 86 } else if (fourexercise[4] == 1) { 87 number.setNum1((int) fourexercise[0]); 88 number.setNum2((int) fourexercise[1]); 89 number.setNum3(String.format("%.0f", fourexercise[2])); 90 number.setNumType("+"); 91 number.setSymbol("="); 92 } else if (fourexercise[4] == 2) { 93 number.setNum1((int) fourexercise[0]); 94 number.setNum2((int) fourexercise[1]); 95 number.setNum3(String.format("%.0f", fourexercise[2])); 96 number.setNumType("*"); 97 number.setSymbol("="); 98 } else { 99 number.setNum1((int) fourexercise[0]); 100 number.setNum2((int) fourexercise[1]); 101 number.setNum3(String.format("%.6f", fourexercise[2])); 102 number.setNum4(String.format("%.6f", fourexercise[3])); 103 number.setNumType("/"); 104 number.setSymbol("="); 105 } 106 exercise.add(number); 107 } 108 return exercise; 109 case num8: 110 Number3 number2 = null; 111 for (int i = 0; i < exerciseNumber; i++) { 112 double[] div2 = division2(num5, num6); 113 number2 = new Number3(); 114 number2.setNum1((int) div2[0]); 115 number2.setNum2((int) div2[1]); 116 number2.setNum3(String.format("%.6f", div2[2])); 117 number2.setNumType("/"); 118 number2.setSymbol("="); 119 exercise.add(number2); 120 } 121 System.out.println(exercise); 122 return exercise; 123 default: 124 break; 125 } 126 return null; 127 }
2.2 实体类Number3
1 public class Number3 { 2 private int num1; 3 private int num2; 4 private String num3; 5 private String num4; 6 private String numType; 7 private String symbol; 8 ..... 9 //此处省略set和get方法 10 }
2.3 action类
1 public class Exercise { 2 private String exerciseType; 3 private String min; 4 private String max; 5 private String exerciseNumber; 6 7 ...... 8 //此处省略get和set方法 9 10 11 public String execute() { 12 ActionContext actioncontext = ActionContext.getContext(); 13 Map<String, Object> session = actioncontext.getSession(); 14 count cou = new count(); 15 List<Number3> exercise = cou.judge(Integer.valueOf(exerciseNumber), Integer.valueOf(exerciseType), 16 Integer.valueOf(min), Integer.valueOf(max)); 17 session.put("exercises", exercise); 18 session.put("exerciseType",exerciseType); 19 if (exerciseType.equals("3")) { 20 return "d"; 21 } else { 22 return "asm"; 23 } 24 } 25 }
3. 运行结果截图
3.1 初始页面
3.2用户输入题目数量和题型
3.3生成题目页面
3.4 尝试填写结果
3.5检测运行结果(检测代码会在之后)
时间: 2024-10-12 05:13:39