Java生成数学计算题,并转成exe可执行文件

这次期末考试,小孩考的太差了,老师要求暑假每天40道口算和10道竖式,内容要求都是两三位数的加减法,在网上太难找了,于是就想着用代码生成pdf文件,方便打印,代码如下:

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MathCalcQuestionGenerator {

    /**
     * 最大数
     */
    private static final int MAX_NUM = 999;
    /**
     * 最小数
     */
    private static final int MIN_NUM = 10;
    /**
     * 每行几道题
     */
    private static final int QUESTION_LINE_NUM = 3;
    /**
     * 口算题数量
     */
    private static final int MENTAL_ARITHMETIC_QUESTIONS_NUM = 40;
    /**
     * 竖式计算题数量
     */
    private static final int VERTICAL_CALCULATION_QUESTIONS_NUM = 10;
    /**
     * 总题目数量
     */
    private static final int TOTAL_NUM;
    /**
     * 开始日期
     */
    private static LocalDate START_DATE = LocalDate.parse("2023-07-01");
    /**
     * 结束日期
     */
    private static LocalDate END_DATE = LocalDate.parse("2023-08-31");

    static {
        TOTAL_NUM = MENTAL_ARITHMETIC_QUESTIONS_NUM + VERTICAL_CALCULATION_QUESTIONS_NUM;
    }

    public static void main(String[] args) throws Exception {

        while (START_DATE.compareTo(END_DATE) <= 0) {
            //生成题目和答案
            List<String> questions = new ArrayList<>(TOTAL_NUM);
            List<String> answers = new ArrayList<>(TOTAL_NUM);
            generateQuestions(questions, answers);
            generateFile("(" + START_DATE + ")数学计算题.pdf", questions);
            generateFile("(" + START_DATE + ")数学计算题(含答案).pdf", answers);
            START_DATE = START_DATE.plusDays(1);
        }
        System.out.println("文件已成功生成!!!");
    }

    /**
     * 生成题目
     *
     * @param questions 题目集合
     * @param answers   答案集合
     */
    private static void generateQuestions(List<String> questions, List<String> answers) {
        Random random = new Random();
        for (int i = 1; i <= MENTAL_ARITHMETIC_QUESTIONS_NUM; i++) {
            generateQuestion(i, random, questions, answers);
        }
        for (int i = 1; i <= VERTICAL_CALCULATION_QUESTIONS_NUM; i++) {
            generateQuestion(i, random, questions, answers);
        }
    }

    /**
     * 生成随机计算题
     *
     * @param i         序号
     * @param random    random对象
     * @param questions 题目
     * @param answers   答案
     */
    private static void generateQuestion(int i, Random random, List<String> questions, List<String> answers) {
        int num1 = random.nextInt(MAX_NUM - MIN_NUM) + MIN_NUM;
        int num2 = random.nextInt(MAX_NUM - MIN_NUM) + MIN_NUM;
        String operator = random.nextBoolean() ? "+" : "-";

        if ("-".equals(operator)) {
            if (num1 < num2) {
                int temp = num1;
                num1 = num2;
                num2 = temp;
            }

            while (num1 - num2 < 0) {
                int temp = num1;
                num1 = num2;
                num2 = temp;
            }
        }

        int result = 0;
        switch (operator) {
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            default:
                break;
        }

        String question = String.format("%2d.%3d %s %3d = _____", i, num1, operator, num2);
        questions.add(question);
        String answer = String.format("%2d.%3d %s %3d = %5d", i, num1, operator, num2, result);
        answers.add(answer);
    }


    /**
     * 生成文件
     *
     * @param fileName  文件名
     * @param questions 题目或者答案
     * @throws Exception
     */
    private static void generateFile(String fileName, List<String> questions) throws Exception {
        Document document = new Document();
        //设置固定宽度字体
        Font font = new Font(BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12, Font.NORMAL);
        //设置中文字体
        Font font2 = new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 20);
        PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        List<String> questionLine = new ArrayList<>();
        //口算题
        document.add(new Paragraph("一、口算题", font2));
        for (int i = 1; i <= MENTAL_ARITHMETIC_QUESTIONS_NUM; i++) {
            String question = questions.get(i - 1);
            questionLine.add(question);
            if (questionLine.size() == QUESTION_LINE_NUM) {
                document.add(new Paragraph(String.join("    ", questionLine), font));
                questionLine.clear();
            }
        }
        if (questionLine.size() > 0) {
            document.add(new Paragraph(String.join("    ", questionLine), font));
            questionLine.clear();
        }
        //空1行
        document.add(new Paragraph(" "));
        //竖式计算题
        document.add(new Paragraph("二、竖式题", font2));

        for (int i = 1; i <= VERTICAL_CALCULATION_QUESTIONS_NUM; i++) {
            String question = questions.get(i + MENTAL_ARITHMETIC_QUESTIONS_NUM - 1);
            questionLine.add(question);
            if (questionLine.size() == QUESTION_LINE_NUM) {
                document.add(new Paragraph(String.join("    ", questionLine), font));
                questionLine.clear();
                //空5行
                document.add(new Paragraph(" "));
                document.add(new Paragraph(" "));
                document.add(new Paragraph(" "));
                document.add(new Paragraph(" "));
                document.add(new Paragraph(" "));
            }
        }
        if (questionLine.size() > 0) {
            document.add(new Paragraph(String.join("    ", questionLine), font));
            questionLine.clear();
        }
        document.close();
    }
}

pom文件中要引入itextpdf

<dependency>
   <groupId>com.itextpdfgroupId>
   <artifactId>itextpdfartifactId>
   <version>5.5.5version>
dependency>
<dependency>
  <groupId>com.itextpdfgroupId>
  <artifactId>itext-asianartifactId>
  <version>5.2.0version>
dependency>

运行main方法就可以在项目根目录生成7月1日到8月31日每天一份题目及答案,内容如下:
Java生成数学计算题,并转成exe可执行文件_第1张图片
Java生成数学计算题,并转成exe可执行文件_第2张图片然后我想着分享给其他家长用,就用Launch4j转exe文件
Launch4j的下载地址:
https://sourceforge.net/projects/launch4j/
安装后运行会出现这样的画面
在Basic下配置这两个
Java生成数学计算题,并转成exe可执行文件_第3张图片在JRE下配置这些Java生成数学计算题,并转成exe可执行文件_第4张图片然后点击小齿轮,填上launch4j
Java生成数学计算题,并转成exe可执行文件_第5张图片最后点击运行按钮就可以了
如果有报错的话,估计是没有设置主清单属性
需要在pom文件中增加如下代码

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-maven-pluginartifactId>
      <version>2.3.7.RELEASEversion>
      <configuration>
        <mainClass>MathCalcQuestionGeneratormainClass>
      configuration>
      <executions>
        <execution>
          <goals>
            <goal>repackagegoal>
          goals>
        execution>
      executions>
    plugin>
  plugins>
build>

最后给上我转好的exe文件

你可能感兴趣的:(Java,自制小程序,java,开发语言,jvm)