随机生成100以内加减混合,每页20道共56页的计算题
public static void main(String[] args) { /** 混合计算 */ homework2(); } public static void homework2() { Random random = new Random(); StringBuffer resultAll = new StringBuffer(); for (int j = 1; j < 57; j++) { StringBuffer res = new StringBuffer(); for (int i = 1; i < 21; i++) { /** 0是加乘 1是加乘 2加减乘 3乘减 4加除 5除加 6减除 7除减 8 括号*/ int strLeng = 0; int type = random.nextInt(9); String s = ""; System.out.println(type+"==========="); if (type == 0 || type == 1) { s = getMultipliceAdd(type + ""); res.append(s); } else if (type == 2 || type == 3) { s = getMultipliceReduce(type + ""); res.append(s); } else if (type == 4 || type == 5) { s = getDivisionAdd(type + ""); res.append(s); } else if (type == 6 || type == 7) { s = getDivisionRed(6 + ""); res.append(s); } else if (type == 8) { s = bracket(); res.append(s); } strLeng = s.length(); if (i > 0 && i % 3 == 0) { res.append("\n"); res.append("\n"); res.append("\n"); res.append("\n"); res.append("\n"); res.append("\n"); }else { for (int k = 0; k < 30-strLeng; k++) { res.append(" "); } } } res.append("\n"); res.append("\n"); res.append("\n"); res.append("\n"); res.append("\n"); res.append("\n"); // res.append(" " + DateUtils.addDayStr(new Date(), j - 3) + " "); res.append(" " + j + " "); res.append("\n"); res.append("\n"); resultAll.append(res); } textToFile("E:\\暑假作业\\100以内加减混合.txt", resultAll.toString()); } /** 括号 */ public static String bracket(){ Random random = new Random(); int type = random.nextInt(4); StringBuffer result = new StringBuffer(); for (int i = 0; i < 500; i++) { int numC = random.nextInt(99); int numA = random.nextInt(99); int numB = random.nextInt(99); if (type == 0 && numA + numB + numC < 101){ result.append(" "); result.append(numA); result.append(" + "); if (numB > numC){ result.append("("); result.append(numB); result.append(" - "); result.append(numC); result.append(")"); }else{ result.append("("); result.append(numB); result.append(" + "); result.append(numC); result.append(")"); } result.append(" = "); break; }else if (type == 1 && (numA + numB - numC) < 101&&(numA + numB - numC) >0){ result.append(" "); result.append(numA); result.append(" + "); if (numB > numC){ result.append("("); } result.append(numB); result.append(" - "); result.append(numC); if (numB > numC){ result.append(")"); } result.append(" = "); break; }else if (type == 2 && numA + numB - numC < 101&& numA + numB - numC >0){ result.append(" "); result.append(numA); result.append(" + "); if (numB > numC){ result.append("("); } result.append(numB); result.append(" - "); result.append(numC); if (numB > numC){ result.append(")"); } result.append(" = "); break; }else if (type == 3 && numC - numB - numA >0){ result.append(" "); result.append(numC); result.append(" - "); result.append("("); result.append(numB); if (numB-numA >0){ result.append(" - "); }else{ result.append(" + "); } result.append(numA); result.append(")"); result.append(" = "); break; } } return result.toString(); } /** * 获取乘法减法算式 */ public static String getMultipliceReduce(String type) { StringBuffer result = new StringBuffer(); Random random = new Random(); for (int j = 0; j < 500; j++) { int numRed = random.nextInt(99); int numA = random.nextInt(9); int numB = random.nextInt(9); if (numA > 0 && numB > 0) { if ("2".equals(type) && numRed >= numA * numB) { result.append(" "); result.append(numRed); result.append(" - "); result.append(numA); result.append(" x "); result.append(numB); result.append(" = "); break; } else if ("3".equals(type) && numRed < numA * numB) { result.append(" "); result.append(numA); result.append(" x "); result.append(numB); result.append(" - "); result.append(numRed); result.append(" = "); break; } } } return result.toString(); } /*乘法加法 */ public static String getMultipliceAdd(String type) { StringBuffer result = new StringBuffer(); Random random = new Random(); for (int j = 0; j < 500; j++) { int numAdd = random.nextInt(99); int numA = random.nextInt(9); int numB = random.nextInt(9); if (numA > 0 && numB > 0 && (numA * numB + numAdd) < 101) { result.append(" "); if ("0".equals(type)) { result.append(numAdd); result.append(" + "); result.append(numA); result.append(" x "); result.append(numB); result.append(" = "); } else { result.append(numA); result.append(" x "); result.append(numB); result.append(" + "); result.append(numAdd); result.append(" = "); } break; } } return result.toString(); } /** * 获取除法减法算式 */ public static String getDivisionRed(String type) { StringBuffer result = new StringBuffer(); Random random = new Random(); int numRed = random.nextInt(50)+50; int numRed2 = random.nextInt(10); for (int j = 0; j < 30000; j++) { int numA = random.nextInt(32)+60; int numB = random.nextInt(10); if (numA==0||numB == 0){ continue; } if (numA / numB > 0 && numA / numB < 10 && numA % numB == 0) { if ("6".equals(type) && numRed - numA / numB >= 0) { result.append(" "); result.append(numRed); result.append(" - "); result.append(numA); result.append(" ÷ "); result.append(numB); result.append(" = "); } else if ("7".equals(type) && numA / numB - numRed2 >= 0) { result.append(numA); result.append(" ÷ "); result.append(numB); result.append(" - "); result.append(numRed2); result.append(" = "); } break; } } return result.toString(); } /** * 获取除法加法算式 */ public static String getDivisionAdd(String type) { StringBuffer result = new StringBuffer(); Random random = new Random(); for (int j = 0; j < 500; j++) { int numAdd = random.nextInt(100); int numA = random.nextInt(82); int numB = random.nextInt(10); if (numA == 0|| numB == 0){ continue; } if (numA / numB > 0 && numA / numB < 10 && numA % numB == 0 && numA / numB + numAdd < 101) { result.append(" "); if ("4".equals(type)) { result.append(numAdd); result.append(" + "); result.append(numA); result.append(" ÷ "); result.append(numB); result.append(" = "); } else if ("5".equals(type)) { result.append(numA); result.append(" ÷ "); result.append(numB); result.append(" + "); result.append(numAdd); result.append(" = "); } break; } } return result.toString(); } public static void textToFile(String path, final String strBuffer) { try { // 创建文件对象 File fileText = new File(path); // 向文件写入对象写入信息 FileWriter fileWriter = new FileWriter(fileText); // 写文件 fileWriter.write(strBuffer); // 关闭 fileWriter.close(); } catch (IOException e) { // e.printStackTrace(); } }