题目:自动随机生成小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。
思路:(1)数据来源:整数 ---> 随机数生成
真分数---> 分子分母都随机产生,保证分子小于分母
四则符号随机---> 从0 ~ 3之间随机取数,取到0 代表“+”,取到1代表“-”,取到2代表“*”,取到3代表“ / ”。
等式左边组合方式随机---> 从0 ~ 2之间取数,取到0代表两个整数做运算,取到1代表一个整数和一个真分数做运算,取到2代表两个真分数做运算。
(2)真分数生成逻辑:随机生成两个整数,比较大小后,大的作为分母,小的作为分子,然后约分后返回字符串类型的值以作为打印显示,分子和分母的值存在一个定义好的对象中,方便其值够的传递
(3)题目生成逻辑:在for循环下,先随机生成两个整数,两个真分数,一个运算符号,一个等式组合方式,然后通过if.. else..判断并打印在每个运算符号下,每一个等式组合方式生成的题目。
(4)结果计算注意点:在减法运算中要注意结果不能为负数;在除法运算中要主以被除数不能为零;在真分数参与的运算中要注意分母不为0且但分母为1时就是分子的值,当分子的值为0时,该真分数的值就是0。
(5)判断题目是否重复的逻辑:将之前用到记录真分数分子分母的类改造,增加两个私有String成员,定义一个类数组,用来记录每次随机产生的两个整数和每次生成的两个分数,然后在每次出题之前利用for循环将当前产生的一道题与之前生成的题比较(判断等式组合方式,运算符号,等式左边的两个数是否同时出现过),如果重复,i减减操作;并结束这次循环。
源代码:
1 package text; 2 import java.util.Scanner; 3 4 class Transmit { 5 private int max; 6 private int min; 7 private String c1; 8 private String c2; 9 public String getC1() { 10 return c1; 11 } 12 public void setC1(String c1) { 13 this.c1 = c1; 14 } 15 public String getC2() { 16 return c2; 17 } 18 public void setC2(String c2) { 19 this.c2 = c2; 20 } 21 public int getMax() { 22 return max; 23 } 24 public void setMax(int max) { 25 this.max = max; 26 } 27 public int getMin() { 28 return min; 29 } 30 public void setMin(int min) { 31 this.min = min; 32 } 33 34 } 35 public class Random { 36 private static int line = 10; 37 /** 38 * 求两个数之间的最大公约数 39 */ 40 public int getGCD(int max, int min) { 41 int gcd = 1; 42 if(min >= max) { 43 int temp = max; 44 max = min; 45 min = temp; 46 } 47 for(int i = min;i>=1;i--) { 48 if(max % i == 0 && min % i == 0) { 49 gcd = i; 50 break; 51 } 52 } 53 return gcd; 54 } 55 56 /** 57 * 生成真分数 58 * @return 一个真分数的字符串 59 */ 60 public String randZ( Transmit transmit) { 61 while(true){ 62 int max =(int)(Math.random()* line); 63 int min =(int)(Math.random()* line); 64 if(max==0 || min==0 || max == min) { //如果有一个为0,或者生成的两个数相等, continue; 65 continue; 66 } else { 67 if(max <= min ) //先保证max的值大于min的值 68 { 69 int temp=min; 70 min=max; 71 max=temp; 72 } 73 //求最大公约数 74 int gcd = getGCD(max, min); 75 max = max / gcd; 76 min = min / gcd; 77 transmit.setMax(max); 78 transmit.setMin(min); 79 return("("+min+"/"+max+")"); 80 } 81 } 82 } 83 84 public String getMark(int midMax, int midMin) { 85 int gcd = getGCD(midMax, midMin);//求最大公约数 86 midMax = midMax / gcd; 87 midMin = midMin / gcd; 88 return(midMin+"/"+midMax); 89 } 90 //求两个真分数相加 91 public String opGrsAdd(Transmit tran1,Transmit tran2) { 92 int midMin = tran1.getMin() * tran2.getMax() + tran1.getMax() * tran2.getMin(); 93 int midMax = tran1.getMax() * tran2.getMax(); 94 int gcd = getGCD(midMax, midMin);//求最大公约数 95 midMax = midMax / gcd; 96 midMin = midMin / gcd; 97 if(midMax == midMin) { 98 return 1+""; 99 }else{ 100 return(midMin+"/"+midMax); 101 } 102 } 103 104 //求两个真分数相乘 105 public String opGrsMult(Transmit tran1, Transmit tran2) { 106 int midMin = tran1.getMin() * tran2.getMin(); 107 int midMax = tran1.getMax()* tran2.getMax(); 108 return getMark(midMax, midMin); 109 } 110 111 //求两个真分数相减 112 public String opGrsSubt(Transmit tran1, Transmit tran2) { 113 int midMin = tran1.getMin() * tran2.getMax() - tran1.getMax() * tran2.getMin(); 114 int midMax = tran1.getMax() * tran2.getMax(); 115 return getMark(midMax, midMin); 116 } 117 118 //求两个真分数相除 119 public String opGrsDivi(Transmit tran1, Transmit tran2) { //tran1被除数,tran2除数 120 int midMin = tran1.getMin() * tran2.getMax(); 121 int midMax = tran1.getMax()* tran2.getMin(); 122 int gcd = getGCD(midMax, midMin); //求最大公约数 123 midMax = midMax / gcd; 124 midMin = midMin / gcd; 125 if(midMax == 1) { 126 return midMin+""; 127 }else { 128 return(midMin+"/"+midMax); 129 } 130 } 131 132 //求一个整数与一个真分数相加 133 public String opGrsAddInt( int n , Transmit tran) { 134 int midMin = n * tran.getMax() +tran.getMin(); 135 int midMax = tran.getMax() ; 136 return getMark(midMax, midMin); 137 } 138 139 //求一个整数与一个真分数相减 140 public String opGrsSubInt(int n, Transmit tran) { 141 int midMin = n * tran.getMax() - tran.getMin(); 142 int midMax = tran.getMax() ; 143 return getMark(midMax, midMin); 144 } 145 146 //求一个整数与一个真分数相乘 147 public String opGrsMulInt(int n, Transmit tran) { 148 int midMin = n * tran.getMin(); 149 int midMax = tran.getMax(); 150 int gcd = getGCD(midMax, midMin);//求最大公约数 151 midMax = midMax / gcd; 152 midMin = midMin / gcd; 153 if(midMin == 0 ) { 154 return 0+""; 155 }else if (midMax == 1) { 156 return midMin+""; 157 }else{ 158 return(midMin+"/"+midMax); 159 } 160 } 161 162 //求一个整数与一个真分数相除 163 public String opGrsSDivInt(int n, Transmit tran) { 164 int midMin = n * tran.getMax(); 165 int midMax = tran.getMin(); 166 int gcd = getGCD(midMax, midMin);//求最大公约数 167 midMax = midMax / gcd; 168 midMin = midMin / gcd; 169 if(midMin == 0 ) { 170 return 0+""; 171 }else if (midMax == 1) { 172 return midMin+""; 173 }else{ 174 return(midMin+"/"+midMax); 175 } 176 } 177 178 public static void main(String[] args) { 179 Scanner input = new Scanner(System.in); 180 Scanner stPut = new Scanner(System.in); 181 Random ran = new Random(); 182 Transmit tran1= new Transmit(); 183 Transmit tran2= new Transmit(); 184 System.out.println("=========================="); 185 System.out.print("请输入出题数量:"); 186 int Num = input.nextInt(); 187 System.out.println("======本次出题数为:"+Num+"道!"+"======"); 188 Transmit sameTran[] = new Transmit[Num+1]; 189 int sameZ[] = new int[Num +1]; 190 int sameSymbol[] = new int[Num +1]; 191 int trueNum = 0; 192 int falseNum = 0; 193 //for循环打印题目 194 for(int i = 1; i<= Num ; i++) { 195 int a =(int)(Math.random()* line); //随机生成两个整数 196 int b =(int)(Math.random()* line); 197 String c1 = ran.randZ(tran1); //随机生成两个真分数 198 String c2 = ran.randZ(tran2); 199 //int symbol = 0; 200 int symbol = (int)(Math.random()*4); //生成 0~3 四个数代表四个运算符号 201 int Z = (int) (Math.random()*3); // 题目有三种情况,(1).两个整数间运算,(2).两个真分数间的运算,(3)真分数与整数间的运算 202 sameTran[i] = new Transmit(); 203 sameTran[i].setMax(a); 204 sameTran[i].setMin(b); 205 sameTran[i].setC1(c1); 206 sameTran[i].setC2(c2); 207 sameSymbol[i] = symbol; 208 sameZ[i] = Z; 209 //判断是否重复 210 for(int j = i; j <= 0; j++) { 211 if(sameTran[i].getMax() == sameTran[j-1].getMax() && sameTran[i].getMin() ==sameTran[j-1].getMin() 212 && sameSymbol[i] ==sameSymbol[j-1] && sameZ[i] == sameZ[j-1]) { 213 i--;continue; 214 }else if(sameTran[i].getMax() ==sameTran[j-1].getMax() && sameTran[i].getC1() ==sameTran[j-1].getC1() 215 && sameSymbol[i] ==sameSymbol[j-1] && sameZ[i] == sameZ[j-1]) { 216 i--;continue; 217 }else if(sameTran[i].getC2() ==sameTran[j-1].getC2() && sameTran[i].getC1() ==sameTran[j-1].getC1() 218 && sameSymbol[i] ==sameSymbol[j-1] && sameZ[i] == sameZ[j-1]) { 219 i--;continue; 220 } 221 } 222 223 if(symbol ==0) { //加法 224 System.out.print("第"+i+"题:"); 225 if(Z == 1) { 226 System.out.print(a +" + " + b +" = " ); 227 int result = input.nextInt(); 228 if(result == (a+b)) { 229 trueNum++; 230 System.out.println("恭喜你,答对了!"); 231 }else { 232 falseNum++; 233 System.out.println("真遗憾,答错了,正确答案是"+(a+b)); 234 } 235 }else if( Z== 2) { 236 System.out.print(a +" + " + c1 +" = " ); 237 String result = stPut.next(); 238 if(result.equals(ran.opGrsAddInt(a, tran1))) { 239 trueNum++; 240 System.out.println("恭喜你,答对了!"); 241 }else { 242 falseNum++; 243 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsAddInt(a, tran1)); 244 } 245 }else if(Z == 0) { 246 System.out.print(c1 +" + " + c2 +" = " ); 247 String result = stPut.next(); 248 if(result.equals(ran.opGrsAdd(tran1, tran2))) { 249 trueNum++; 250 System.out.println("恭喜你,答对了!"); 251 }else { 252 falseNum++; 253 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsAdd(tran1, tran2)); 254 } 255 } 256 }else if(symbol == 1 ){ //减法 257 if(Z == 1) { 258 if((a-b) < 0 ) { 259 i--; continue; 260 }else{ 261 System.out.print("第"+i+"题:"); 262 System.out.print(a +" - " + b +" = " ); 263 int result = input.nextInt(); 264 if(result == (a-b)) { 265 trueNum++; 266 System.out.println("恭喜你,答对了!"); 267 }else { 268 falseNum++; 269 System.out.println("真遗憾,答错了,正确答案是"+(a-b)); 270 } 271 } 272 }else if( Z== 2) { 273 if(( a * tran1.getMax() - tran1.getMin() ) < 0) { 274 i--; continue; 275 }else { 276 System.out.print("第"+i+"题:"); 277 System.out.print(a +" - " + c1 +" = " ); 278 String result = stPut.next(); 279 if(result.equals(ran.opGrsSubInt(a, tran1))) { 280 trueNum++; 281 System.out.println("恭喜你,答对了!"); 282 }else { 283 falseNum++; 284 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsSubInt(a, tran1)); 285 } 286 } 287 }else if(Z == 0) { 288 if(( tran1.getMin() * tran2.getMax() - tran1.getMax() * tran2.getMin() ) < 0 ) { 289 i--; continue; 290 }else{ 291 System.out.print("第"+i+"题:"); 292 if(c1.equals(c2)) { 293 System.out.print(c1 +" - " + c2 +" = "); 294 String result = stPut.next(); 295 if(result.equals("0")) { 296 trueNum++; 297 System.out.println("恭喜你,答对了!"); 298 }else { 299 falseNum++; 300 System.out.println("真遗憾,答错了,正确答案是"+"0"); 301 } 302 }else{ 303 System.out.print(c1 +" - " + c2 +" = " ); 304 String result = stPut.next(); 305 if(result.equals(ran.opGrsSubt(tran1, tran2))) { 306 trueNum++; 307 System.out.println("恭喜你,答对了!"); 308 }else { 309 falseNum++; 310 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsSubt(tran1, tran2)); 311 } 312 } 313 } 314 } 315 }else if(symbol == 2){ //乘法 316 System.out.print("第"+i+"题:"); 317 if(Z == 1) { 318 System.out.print(a +" × " + b +" = " ); 319 int result = input.nextInt(); 320 if(result == (a*b)) { 321 trueNum++; 322 System.out.println("恭喜你,答对了!"); 323 }else { 324 falseNum++; 325 System.out.println("真遗憾,答错了,正确答案是"+(a*b)); 326 } 327 }else if( Z== 2) { 328 System.out.print(a +" × " +c1 +" = " ); 329 String result = stPut.next(); 330 if(result.equals(ran.opGrsMulInt(a, tran1))) { 331 trueNum++; 332 System.out.println("恭喜你,答对了!"); 333 }else { 334 falseNum++; 335 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsMulInt(a, tran1)); 336 } 337 }else if(Z == 0) { 338 System.out.print(c1 +" × " + c2 +" = " ); 339 String result = stPut.next(); 340 if(result.equals(ran.opGrsMult(tran1, tran2))) { 341 trueNum++; 342 System.out.println("恭喜你,答对了!"); 343 }else { 344 falseNum++; 345 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsMult(tran1, tran2)); 346 } 347 } 348 }else if(symbol == 3 ){ // 除法 349 if(Z == 1 ) { 350 if(b == 0) { 351 i--; continue; 352 }else if (a == 0) { 353 System.out.print("第"+i+"题:"); 354 System.out.print(a + " ÷ " + b +" = " ); 355 String result = stPut.next(); 356 if(result.equals("0")) { 357 trueNum++; 358 System.out.println("恭喜你,答对了!"); 359 }else { 360 falseNum++; 361 System.out.println("真遗憾,答错了,正确答案是"+"0"); 362 } 363 }else { 364 System.out.print("第"+i+"题:"); 365 int gcd = ran.getGCD(b, a); 366 int A = a / gcd; int B = b / gcd; 367 if(B == 1) { 368 System.out.print(a + " ÷ " + b +" = " ); 369 String result = stPut.next(); 370 if(result.equals(A)) { 371 trueNum++; 372 System.out.println("恭喜你,答对了!"); 373 }else { 374 falseNum++; 375 System.out.println("真遗憾,答错了,正确答案是"+A); 376 } 377 }else{ 378 System.out.print(a + " ÷ " + b +" = " ); 379 String result = stPut.next(); 380 if(result.equals(A+"/"+B)) { 381 trueNum++; 382 System.out.println("恭喜你,答对了!"); 383 }else { 384 falseNum++; 385 System.out.println("真遗憾,答错了,正确答案是"+(A+"/"+B)); 386 } 387 } 388 } 389 }else if( Z== 2) { 390 System.out.print("第"+i+"题:"); 391 System.out.print(a + " ÷ " + c1 +" = " ); 392 String result = stPut.next(); 393 if(result.equals(ran.opGrsSDivInt(a, tran1))) { 394 trueNum++; 395 System.out.println("恭喜你,答对了!"); 396 }else { 397 falseNum++; 398 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsSDivInt(a, tran1)); 399 } 400 }else if(Z == 0) { 401 System.out.print("第"+i+"题:"); 402 System.out.print(c1 + " ÷ " + c2 +" = " ); 403 String result = stPut.next(); 404 if(result.equals(ran.opGrsDivi(tran1, tran2))) { 405 trueNum++; 406 System.out.println("恭喜你,答对了!"); 407 }else { 408 falseNum++; 409 System.out.println("真遗憾,答错了,正确答案是"+ran.opGrsDivi(tran1, tran2)); 410 } 411 } 412 } 413 if(i == 10){ 414 System.out.println("=========================="); 415 System.out.println("答题结束!本次共"+Num+"道题,你答对了"+trueNum+"道题,答错了"+falseNum+"道题!"); 416 } 417 } 418 } 419 }
运行截图: