主函数:
1 package h2; 2 3 import java.text.DecimalFormat; 4 import java.util.Scanner; 5 import java.util.regex.Pattern; 6 7 import Excep.YichangException; 8 /* 9 * 1.定制数量 10 * 2.控制是否有乘除法 11 * 3.控制数值范围 12 * 4.定制真分数练习题 13 * 5.校检用户输入 14 * 6.输出答题正确率 15 */ 16 public class Main { 17 public static int max = 10;// 控制算式个数 18 public static String[] staticanser = new String[max];// 标准答案 19 public static DecimalFormat decimal = new DecimalFormat("#.##"); 20 21 22 // 求最大公约数 23 private static int calcMaxSubmultiple(int num1, int num2) { 24 num1=Math.abs(num1);//防止负数时求不得最大公约数。 25 num2=Math.abs(num2); 26 int min = Math.min(num1, num2); 27 int maxSubmultiple = 1; 28 for (int i = min; i >= 1; i--) { 29 if (num1 % i == 0 && num2 % i == 0) 30 { 31 maxSubmultiple = i; 32 break; 33 } 34 35 } 36 return maxSubmultiple; 37 } 38 //主函数 39 public static void main(String[] args) { 40 41 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 42 int[] no = new int[4];// 操作符地址 43 int useno = 0;// 控制操作符 44 int n = 3;// 操作数个数(随机) 45 int[] num1 = new int[10];// 操作数 46 char opp;// 判断是否需要乘除法 47 char real;// 判断是否需要真分数的题目 48 int[] cs = { 1, 100 };// 数值范围 49 String[] useranser = new String[max];// 用户输入的答案 50 51 52 int f = 0;// 控制输出真分数的操作符 53 int count = 0;// 统计答题正确的数量 54 55 int s1 = 1;// 分子通分 56 int ss1 = 1;// 分子通分 57 int s2 = 1;// 分母通分 58 int result = 0;// 分子计算 59 int gys;// 最大公约数 60 int ff = 0;// 分数除法,分子为0标志位 61 int fff=0;// 62 String zjfz = new String();// 最简分子 63 String zjfm = new String();// 最简分母 64 Pattern pattern = Pattern.compile("[0-9]*"); // 限定输入算式数量输入的必须是数字 65 66 Scanner in = new Scanner(System.in); 67 //定制要求 68 System.out.print("请输入需定制的算式数量(1-20):");// 1.定制数量 69 do { 70 String str = in.nextLine(); 71 if (pattern.matcher(str).matches()) {// 如果输入的是数字就执行 72 max = Integer.parseInt(str); 73 if(max<=20&&max>0) 74 { 75 break; 76 } 77 else 78 { 79 System.out.print("你输入的范围有误,请重新输入:"); 80 } 81 82 } else { 83 System.out.print("你输入的不是数字,请重新输入:"); 84 } 85 } while (true); 86 87 88 System.out.print("是否需要乘除法(Y/N):");// 2.控制乘除参数 89 do { 90 opp = in.next().charAt(0); 91 if (opp == 'Y' || opp == 'y') { 92 useno = 4; 93 break; 94 } else if (opp == 'N' || opp == 'n') { 95 useno = 2; 96 break; 97 } else { 98 System.out.print("输入错误,请重新输入:"); 99 } 100 } while (true); 101 102 System.out.print("参数范围(eg:1,100):");// 3.控制数值范围 103 String str = new String(); 104 int sr = 0;//判断循环结束 105 in.nextLine();// 过滤掉上面.next()方面的回车。 106 do { 107 try { 108 str = in.nextLine(); 109 String[] ss = new String[2]; 110 ss = str.split(","); 111 cs[0] = Integer.valueOf(ss[0]); 112 cs[1] = Integer.valueOf(ss[1]); 113 sr = 1; 114 } catch (Exception e) { 115 System.out.print("输入格式错误,请重新输入:"); 116 } 117 } while (sr != 1); 118 119 120 System.out.print("是否增加真分数练习题(Y/N):");// 4.真分数题目 121 do { 122 real = in.next().charAt(0); 123 if ( real == 'Y' || real == 'y') { 124 125 break; 126 } else if ( real == 'N' || real == 'n') { 127 break; 128 } else { 129 System.out.print("输入错误,请重新输入:"); 130 } 131 } while (true); 132 133 //----------------题目----------------------- 134 System.out.println(); 135 System.out.println(" 2014-2015学年度第一单元测试卷"); 136 System.out 137 .println("班级: 姓名: 座号: 得分: "); 138 System.out.println(); 139 System.out.println("一、请认真仔细地计算下面各题。(小数请保留小数点后两位)"); 140 System.out.println(); 141 Core c1=new Core(); 142 for (int i = 0; i < max; i++) { 143 144 System.out.print("(" + (i + 1) + ") "); 145 n = (int) (Math.random() * 4 + 2);// 2-5个操作数 146 for (int j = 0; j < n; j++) { 147 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值 148 } 149 for (int k = 0; k < n - 1; k++) { 150 no[k] = (int) (Math.random() * useno + 1);// 随机产生操作符 151 if(no[k]==4&&num1[k+1]==0){ 152 do{ 153 num1[k+1]=(int) (Math.random() * (cs[1] - cs[0]) + cs[0]);//如果除号后为0,则重新取数。 154 }while(num1[k+1]==0); 155 } 156 } 157 for (int h = 0; h < n; h++) { 158 if (h != n - 1) { 159 if(num1[h]<0) 160 { 161 System.out.print("("+num1[h]+")"); 162 System.out.print(op[no[h]]); 163 } 164 else{ 165 System.out.print(num1[h]); 166 System.out.print(op[no[h]]); 167 } 168 } else { 169 if(num1[h]<0) 170 { 171 System.out.print("("+num1[h]+")="); 172 } 173 else 174 System.out.print(num1[h] + "="); 175 } 176 } 177 System.out.println(); 178 c1.calcute(num1, op, no, n); 179 } 180 181 182 183 184 185 186 // 学生答题模块 187 System.out.println("==================答题分割线========================="); 188 for (int i = 0; i < max; i++) { 189 System.out.print((i + 1) + ":"); 190 useranser[i]=in.next(); 191 if (useranser[i].equalsIgnoreCase(staticanser[i])) { 192 count++; 193 } 194 } 195 System.out.println("标准答案为:"); 196 for (int i = 0; i < max; i++) { 197 System.out.println((i + 1) + ":" + staticanser[i]); 198 } 199 System.out.println("答题正确率为:" + String.valueOf(decimal.format(((float) count / (float) max) * 100)) 200 + "%"); 201 System.out.println("==================答题分割线========================="); 202 203 //真分数 204 if (real == 'Y' || real == 'y') { 205 System.out.println("二、请计算下列真分数算式。(无法计算请填null)"); 206 System.out.println(); 207 for (int i = 0; i < max; i++) { 208 System.out.print("(" + (i + 1) + ") "); 209 for (int j = 0; j < 2; j++)// (第一个真分数) 210 { 211 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值 212 if (j == 1 ) { 213 while (num1[j - 1] >num1[j]||num1[j]== 0) { 214 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值 215 } 216 } 217 } 218 for (int j = 2; j < 4; j++)// (第二个真分数) 219 { 220 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值 221 if (j == 3) { 222 while (num1[j - 1] >num1[j]||num1[j]== 0) { 223 num1[j] = (int) (Math.random() * (cs[1] - cs[0]) + cs[0]);// 控制随机数数值 224 } 225 } 226 } 227 228 for (int k = 0; k < 1; k++) {// 符号个数 229 no[k] = (int) (Math.random() * useno + 1);// 随机产生操作符 230 } 231 for (int h = 0; h < 4; h++) {// 2个真分数 232 if (h % 2 == 0) 233 System.out.print(("(" + num1[h] + "/")); 234 else if (h % 2 == 1) { 235 System.out.print(num1[h] + ")"); 236 if (f < 1) {// 控制只输出一个操作符 237 System.out.print(op[no[f]]); 238 f++; 239 } else 240 System.out.println("="); 241 242 } 243 } 244 245 f = 0; 246 //计算第二大题标准答案 247 count=0; 248 249 } 250 251 // 答题模板 252 System.out.println("==================答题分割线========================="); 253 for (int i = 0; i < max; i++) { 254 System.out.print((i + 1) + ":"); 255 useranser[i] = in.next(); 256 if (useranser[i].equals(staticanser[i])) { 257 count++; 258 } 259 } 260 System.out.println("标准答案为:"); 261 for (int i = 0; i < max; i++) { 262 System.out.println((i + 1) + ":" + staticanser[i]); 263 } 264 System.out.println("答题正确率为:" + String.valueOf(decimal.format(((float) count / (float) max) * 100)) 265 + "%"); 266 System.out 267 .println("==================答题分割线========================="); 268 } 269 270 } 271 }