一、需求
实现输入出题数值的大小、答案的大小、出题的个数、每行打印的个数、每道题参与运算数字的个数、填空位置是否随机,输出题目。
二、代码实现
import java.util.*; public class CalNum { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入出题的最大值:"); int numMax = scanner.nextInt(); System.out.println("请输入答案的最大值:"); int resultMax = scanner.nextInt(); System.out.println("请输入参与计算的数字的个数(2+3=5,个数为2):"); int preNum = scanner.nextInt(); if (preNum < 2) { preNum = 2; } System.out.println("是否随机填空(1:是,0:否):"); int isRandom = scanner.nextInt(); System.out.println("请输入出题的个数:"); int questionNum = scanner.nextInt(); System.out.println("请输入每行打印的个数:"); int printNum = scanner.nextInt(); List<List<String>> printQuestList = new ArrayList<>(); getQuest(numMax, resultMax, preNum, questionNum, printQuestList); printQuest(isRandom, printNum, printQuestList); } private static void printQuest(int isRandom, int printNum, List<List<String>> printQuestList) { int count = 0; int lineNum = 0; for (List<String> itemQuest : printQuestList) { int itemSize = itemQuest.size(); if (isRandom != 1) { itemQuest.set(itemSize - 1, "( )"); } else { int randomIndex = new Random().nextInt(itemSize); if (randomIndex % 2 == 0) { itemQuest.set(randomIndex, "( )"); } else { itemQuest.set(randomIndex - 1, "( )"); } } for (String item : itemQuest) { System.out.format("%-3s", item); } System.out.format("%3s", " "); count++; if (count % printNum == 0) { System.out.println(); lineNum++; if (lineNum % 4 == 0) { System.out.println(); } } } } private static void getQuest(int numMax, int resultMax, int preNum, int questionNum, List<List<String>> printQuest) { while (questionNum > 0) { // 出题 List<String> preQuetionOper = new ArrayList<>(); List<Integer> preQuetionNum = new ArrayList<>(); while (preQuetionNum.size() < preNum) { Integer temNum = getNum(numMax); if (!preQuetionNum.contains(temNum)) { preQuetionNum.add(temNum); } } Integer result = preQuetionNum.get(0); for (int i = 1; i < preQuetionNum.size(); i++) { Integer oper = new Random().nextInt(2); if (oper == 0) { result += preQuetionNum.get(i); preQuetionOper.add("+"); } else { result -= preQuetionNum.get(i); preQuetionOper.add("-"); } } if (result <= resultMax && result > 0) { List<String> prePrintQuest = new ArrayList<>(); prePrintQuest.add(preQuetionNum.get(0).toString()); for (int i = 1; i < preQuetionNum.size(); i++) { prePrintQuest.add(preQuetionOper.get(i - 1).toString()); prePrintQuest.add(preQuetionNum.get(i).toString()); } prePrintQuest.add("="); prePrintQuest.add(result.toString()); printQuest.add(prePrintQuest); questionNum--; } } } /** * 获取一个随机数 * * @param num * @return */ public static Integer getNum(int num) { return new Random().nextInt(num) + 1; } }
三、演示
请输入出题的最大值: 20 请输入答案的最大值: 20 请输入参与计算的数字的个数(2+3=5,个数为2): 2 是否随机填空(1:是,0:否): 1 请输入出题的个数: 200 请输入每行打印的个数: 4 ( )- 4 = 7 ( )+ 6 = 16 5 - ( )= 2 2 + ( )= 15 14 + 2 = ( ) 8 + ( )= 11 4 + ( )= 16 ( )- 3 = 8 ( )- 2 = 2 13 + ( )= 19 20 - ( )= 9 20 - ( )= 7 ( )+ 3 = 7 2 + ( )= 16 ( )+ 15 = 20 6 - ( )= 4 13 - ( )= 7 1 + 8 = ( ) ( )+ 15 = 16 ( )+ 12 = 20 ( )- 4 = 12 ( )+ 3 = 7 ( )+ 1 = 12 ( )+ 14 = 19 ( )- 10 = 3 ( )- 14 = 4 ( )+ 14 = 19 17 - ( )= 12 11 + ( )= 14 ( )- 14 = 6 9 - 6 = ( ) 9 - ( )= 4 10 - ( )= 9 18 - ( )= 14 15 - ( )= 5 5 + ( )= 15 ( )+ 6 = 11 16 - 7 = ( ) ( )+ 9 = 16 18 - ( )= 5 4 + 13 = ( ) 1 + ( )= 3 14 - ( )= 12 4 + ( )= 5 7 + ( )= 18 ( )- 2 = 17 14 - ( )= 6 18 - 11 = ( ) 2 + ( )= 14 ( )+ 8 = 15 5 + ( )= 15 6 - ( )= 4 13 - 12 = ( ) 10 + ( )= 15 ( )- 3 = 4 8 - 2 = ( ) 13 - ( )= 7 18 - ( )= 14 8 - ( )= 1 ( )- 6 = 6 17 - ( )= 3 13 - ( )= 11 ( )+ 6 = 9 19 - 9 = ( ) 4 + ( )= 11 ( )+ 8 = 9 13 - ( )= 7 16 - ( )= 11 ( )+ 13 = 14 13 - 2 = ( ) ( )+ 6 = 14 ( )+ 8 = 19 14 - ( )= 4 15 - 1 = ( ) ( )+ 4 = 15 8 - ( )= 7 ( )+ 4 = 17 12 - ( )= 9 ( )- 8 = 7 8 + ( )= 19 6 - 5 = ( ) 5 + ( )= 11 15 + 4 = ( ) 15 - ( )= 7 ( )- 4 = 3 ( )+ 14 = 20 19 - 13 = ( ) 13 - 7 = ( ) ( )+ 2 = 15 ( )- 9 = 5 14 - ( )= 7 5 - 4 = ( ) ( )- 14 = 1 11 - 8 = ( ) ( )- 7 = 1 18 + ( )= 20 ( )- 8 = 11 1 + 12 = ( ) 8 + ( )= 12 ( )+ 10 = 12 ( )- 14 = 5 ( )+ 15 = 20 ( )- 12 = 3 ( )- 4 = 14 14 - ( )= 2 ( )- 7 = 7 ( )- 5 = 3 15 - ( )= 14 14 - ( )= 1 13 - ( )= 5 2 + ( )= 13 6 + ( )= 10 20 - 14 = ( ) 8 - ( )= 7 6 + ( )= 18 ( )- 8 = 1 15 - ( )= 12 4 + 1 = ( ) 6 - ( )= 5 ( )- 9 = 11 ( )+ 12 = 13 15 - ( )= 14 ( )- 1 = 16 8 + 4 = ( ) ( )+ 9 = 19 ( )- 3 = 15 13 - ( )= 2 12 + 6 = ( ) 11 + ( )= 14 8 + ( )= 18 ( )- 19 = 1 ( )+ 13 = 15 ( )- 17 = 2 18 + ( )= 19 18 - 3 = ( ) 20 - ( )= 5 6 + ( )= 14 6 + ( )= 19 ( )+ 9 = 11 13 - 11 = ( ) 7 + ( )= 20 15 - 10 = ( ) 4 + ( )= 6 ( )- 8 = 7 16 + ( )= 18 ( )- 3 = 15 14 - ( )= 2 ( )+ 3 = 7 6 + ( )= 17 ( )- 13 = 1 ( )- 6 = 1 9 - ( )= 3 ( )- 7 = 6 ( )- 1 = 14 9 + ( )= 17 7 + ( )= 11 18 - ( )= 12 ( )+ 2 = 9 8 + 6 = ( ) 11 - ( )= 3 6 - ( )= 3 ( )- 3 = 1 7 + ( )= 16 8 - ( )= 1 16 - 5 = ( ) 12 - ( )= 9 7 + ( )= 18 ( )+ 6 = 8 8 + 1 = ( ) ( )+ 6 = 8 6 - ( )= 5 8 - 6 = ( ) ( )+ 8 = 9 ( )+ 10 = 17 14 - ( )= 3 ( )- 4 = 10 ( )- 9 = 8 16 - ( )= 1 20 - 2 = ( ) 2 + ( )= 10 19 - ( )= 8 20 - 13 = ( ) ( )+ 3 = 14 8 - ( )= 4 ( )+ 8 = 11 20 - 9 = ( ) 17 - ( )= 12 1 + 2 = ( ) 11 - ( )= 4 15 - ( )= 12 10 - ( )= 1 7 + ( )= 8 ( )- 14 = 5 4 + ( )= 12 15 - ( )= 11 ( )- 7 = 8 7 + ( )= 20 ( )+ 3 = 14 13 + 5 = ( ) 16 - 12 = ( ) 进程已结束,退出代码0
请输入出题的最大值: 50 请输入答案的最大值: 50 请输入参与计算的数字的个数(2+3=5,个数为2): 5 是否随机填空(1:是,0:否): 1 请输入出题的个数: 50 请输入每行打印的个数: 4 29 - ( )- 8 + 16 + 14 = 32 ( )+ 28 - 49 - 25 + 50 = 28 ( )+ 8 - 9 - 29 + 2 = 9 18 - 14 + 41 + ( )- 34 = 48 ( )- 22 + 1 + 16 + 8 = 41 ( )- 1 + 35 - 37 + 9 = 32 25 + 26 - ( )+ 13 - 12 = 18 6 + 2 + ( )+ 31 - 40 = 42 34 + ( )- 4 - 29 + 1 = 30 41 - 50 + 38 - ( )+ 23 = 27 ( )+ 12 - 29 + 16 - 4 = 26 ( )+ 35 - 30 + 38 - 50 = 40 ( )+ 15 + 6 - 3 + 14 = 33 46 + 47 - ( )+ 25 - 35 = 42 33 + 32 + 7 + 4 - ( )= 40 ( )- 38 + 27 + 44 - 43 = 40 37 - ( )- 40 + 21 + 33 = 25 44 + 6 - 26 - ( )+ 29 = 43 50 - 11 - 34 + 25 - ( )= 8 5 + 41 - ( )+ 29 - 12 = 43 23 - 37 + 50 - 7 - 2 = ( ) 15 + ( )- 29 - 42 + 48 = 11 38 + 37 - 10 - 1 - 48 = ( ) 15 + 2 + 16 - ( )+ 22 = 43 15 - 16 + 50 + ( )- 38 = 48 29 - 38 + 49 + 48 - 45 = ( ) 24 - 29 + ( )- 3 - 11 = 2 45 + 9 - ( )- 26 + 14 = 39 42 + ( )- 12 - 23 + 11 = 19 46 + 28 - 23 - 9 - ( )= 12 18 - 8 + 24 - 49 + ( )= 33 45 - 6 - 26 + 17 - ( )= 17 5 + 46 + 15 + 18 - ( )= 49 ( )+ 20 - 24 - 3 - 15 = 17 37 - 35 - 38 + 34 + 30 = ( ) 50 + ( )- 13 - 19 - 10 = 48 2 + 16 - ( )+ 10 + 33 = 33 8 + 4 - ( )- 2 + 27 = 30 39 - 3 + 4 - 30 + ( )= 26 38 - 12 - 47 + ( )+ 10 = 22 2 + 48 + 1 - ( )+ 6 = 18 48 - 4 - ( )- 15 + 19 = 45 3 + 19 - 8 - 37 + 36 = ( ) 28 + 31 + 39 - 48 - ( )= 29 21 + 29 - 45 + 14 + 26 = ( ) 10 - 20 + 12 - 11 + 32 = ( ) 32 - 8 - ( )+ 5 + 23 = 11 ( )- 6 + 48 - 23 - 35 = 26 43 - 20 - 4 - 9 + 38 = ( ) 11 + 26 - ( )- 9 + 28 = 37 进程已结束,退出代码0