目 录CONTENT

文章目录

基于Apache POI随机生成儿子的合成分解算术题

成培培
2024-11-21 / 0 评论 / 0 点赞 / 37 阅读 / 0 字

需求来源

最近幼儿园老师总在要求家长给孩子出10以内的合成分解题目,类似这个:

就很麻烦所以想到能不能写个代码直接生成题目,然后打印出来给他做就好了,方便快捷,想到就做

实现方案

实现方式也很简单,题目很规整可以直接放Excel文件里,直接用Apache POI操作单元格即可,思路就这么几步:

  1. 根据A4纸的长款规划好单元格的大小以及题目的个数

  2. 随机生成一个10以内的随机数n,然后生成一个n以内的随机数a,10 - a得到b,这样一道题目的3个数得到了

  3. 生成步骤2这样的50道题目,然后循环将题目输出到Excel即可

具体代码如下:

import cn.hutool.core.util.RandomUtil;
import lombok.Data;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        // 准备好各个单元格需要用到的样式
        CellStyle cellStyleOne = workbook.createCellStyle();
        cellStyleOne.setAlignment(HorizontalAlignment.LEFT);
        CellStyle cellStyleTwo = workbook.createCellStyle();
        cellStyleTwo.setAlignment(HorizontalAlignment.RIGHT);
        CellStyle cellStyleTotal = workbook.createCellStyle();
        cellStyleTotal.setAlignment(HorizontalAlignment.CENTER);
        CellStyle cellStyleNull = workbook.createCellStyle();
        cellStyleNull.setBorderBottom(BorderStyle.THIN);
        cellStyleNull.setBorderTop(BorderStyle.THIN);
        cellStyleNull.setBorderLeft(BorderStyle.THIN);
        cellStyleNull.setBorderRight(BorderStyle.THIN);
        // 设置所有单元格的宽度
        for (int i = 0; i < 32; i++) {
            sheet.setColumnWidth(i, 700);
        }
        // 一次创建24行44列的单元格,放在Map里方便后续调用
        Map<String, XSSFCell> cellMap = new HashMap<>();
        for (int i = 0; i < 48; i++) {
            XSSFRow row = sheet.createRow(i);
            row.setHeight((short) 300);
            for (int j = 0; j < 32; j++) {
                XSSFCell cell = row.createCell(j);
                cellMap.put(i + "," + j, cell);
            }
        }
        // 生成50道题目
        List<Topic> topicList = Topic.createTopic(120, max);
        int index = 0;
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 8; j++) {
                Topic topic = topicList.get(index++);
                if (topic.getType() == 0) {
                    // 生成合成题
                    XSSFCell cell1 = cellMap.get((i * 4) + "," + (j * 4));
                    cell1.setCellValue(topic.getOne());
                    cell1.setCellStyle(cellStyleOne);
                    XSSFCell cell2 = cellMap.get((i * 4) + "," + (j * 4 + 2));
                    cell2.setCellValue(topic.getTwo());
                    cell2.setCellStyle(cellStyleTwo);
                    XSSFCell cell3 = cellMap.get((i * 4 + 2) + "," + (j * 4 + 1));
                    cell3.setCellValue(topic.getTotal());
                    cell3.setCellStyle(cellStyleTotal);
                    XSSFCell cell4 = cellMap.get((i * 4 + 1) + "," + (j * 4 ));
                    cell4.setCellValue("\\");
                    cell4.setCellStyle(cellStyleTwo);
                    XSSFCell cell5 = cellMap.get((i * 4 + 1) + "," + (j * 4 + 2));
                    cell5.setCellValue("/");
                    cell5.setCellStyle(cellStyleOne);
                    // 随机将3个数值单元格中的一个清空作为题目
                    switch (topic.getNullPosition()) {
                        case 0:
                            cell3.setCellValue("");
                            cell3.setCellStyle(cellStyleNull);
                            break;
                        case 1:
                            cell1.setCellValue("");
                            cell1.setCellStyle(cellStyleNull);
                            break;
                        case 2:
                            cell2.setCellValue("");
                            cell2.setCellStyle(cellStyleNull);
                            break;
                    }
                } else {
                    // 生成分解题
                    XSSFCell cell1 = cellMap.get((i * 4 + 2) + "," + (j * 4));
                    cell1.setCellValue(topic.getOne());
                    cell1.setCellStyle(cellStyleOne);
                    XSSFCell cell2 = cellMap.get((i * 4 + 2) + "," + (j * 4 + 2));
                    cell2.setCellValue(topic.getTwo());
                    cell2.setCellStyle(cellStyleTwo);
                    XSSFCell cell3 = cellMap.get((i * 4) + "," + (j * 4 + 1));
                    cell3.setCellValue(topic.getTotal());
                    cell3.setCellStyle(cellStyleTotal);
                    XSSFCell cell4 = cellMap.get((i * 4 + 1) + "," + (j * 4));
                    cell4.setCellValue("/");
                    cell4.setCellStyle(cellStyleTwo);
                    XSSFCell cell5 = cellMap.get((i * 4 + 1) + "," + (j * 4 + 2));
                    cell5.setCellValue("\\");
                    cell5.setCellStyle(cellStyleOne);
                    // 随机将3个数值单元格中的一个清空作为题目
                    switch (topic.getNullPosition()) {
                        case 0:
                            cell3.setCellValue("");
                            cell3.setCellStyle(cellStyleNull);
                            break;
                        case 1:
                            cell1.setCellValue("");
                            cell1.setCellStyle(cellStyleNull);
                            break;
                        case 2:
                            cell2.setCellValue("");
                            cell2.setCellStyle(cellStyleNull);
                            break;
                    }
                }
            }
        }

        // 输出文件
        try (OutputStream os = Files.newOutputStream(Paths.get("/Users/chengpei/Desktop/test.xlsx"))) {
            workbook.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workbook.close();
        }
    }

    @Data
    public static class Topic {

        /**
         * 0-合并 1-分解
         */
        private int type;

        /**
         * 0-空总值 1-空左值 2-空又值
         */
        private int nullPosition;

        private int one;

        private int two;

        private int max;

        private int getTotal() {
            return one + two;
        }

        public static List<Topic> createTopic(int n, int max) {
            List<Topic> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(createTopic(max));
            }
            return list;
        }

        public static Topic createTopic(int max) {
            Topic topic = new Topic();
            topic.setMax(max);
            int total = RandomUtil.randomInt(3, max + 1);
            topic.setOne(RandomUtil.randomInt(1, total));
            topic.setTwo(total - topic.getOne());
            topic.setNullPosition(RandomUtil.randomInt(0, 3));
            topic.setType(RandomUtil.randomInt(0, 2));
            return topic;
        }
    }
}

主要就用到了以下两个依赖:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.2</version>
</dependency>

一个hutool用来生成随机数,一个poi用来操作生成Excel文件,生成的文件打印预览效果如图:

如果只是需要题目可以在我的博客(https://www.chengpei.top/)直接下载随机的题目Excel,首页-常用工具-下载合成分解练习题即可下载到

0

评论区