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

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路Java生成数学计算题,并转成exe可执行文件,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

这次期末考试,小孩考的太差了,老师要求暑假每天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.itextpdf</groupId>
   <artifactId>itextpdf</artifactId>
   <version>5.5.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itext-asian</artifactId>
  <version>5.2.0</version>
</dependency>

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

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.3.7.RELEASE</version>
      <configuration>
        <mainClass>MathCalcQuestionGenerator</mainClass>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

最后给上我转好的exe文件

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/154474.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!