Browse Source

第一次

zhangkaikai 2 years ago
commit
f1ebbb78aa
4 changed files with 219 additions and 0 deletions
  1. 4 0
      .gitignore
  2. 30 0
      pom.xml
  3. 66 0
      src/main/java/com/kk/LotteryDrawing.java
  4. 119 0
      src/main/java/com/kk/LotteryTicketGenerator.java

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+.idea/
+*.iml
+target/
+

+ 30 - 0
pom.xml

@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.example</groupId>
+    <artifactId>caipiao</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <!-- Other dependencies -->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <version>5.2.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>5.2.0</version>
+        </dependency>
+    </dependencies>
+
+</project>

+ 66 - 0
src/main/java/com/kk/LotteryDrawing.java

@@ -0,0 +1,66 @@
+package com.kk;
+
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class LotteryDrawing {
+
+    public static List<Integer> generateLotteryNumbers(int quantity, int minValue, int maxValue) {
+        List<Integer> numbers = new ArrayList<>();
+        for (int i = minValue; i <= maxValue; i++) {
+            numbers.add(i);
+        }
+
+        Collections.shuffle(numbers);
+        List<Integer> lotteryNumbers = numbers.subList(0, quantity);
+        Collections.sort(lotteryNumbers);
+
+        return lotteryNumbers;
+    }
+
+    public static void exportToExcel(List<Integer> lotteryNumbers, String fileName) throws IOException {
+        Workbook workbook = new XSSFWorkbook();
+        Sheet sheet = workbook.createSheet("Lottery Numbers");
+
+        Row headerRow = sheet.createRow(0);
+        headerRow.createCell(0).setCellValue("中奖号码");
+
+        Row dataRow = sheet.createRow(1);
+        for (int i = 0; i < lotteryNumbers.size(); i++) {
+            dataRow.createCell(i).setCellValue(lotteryNumbers.get(i));
+        }
+
+        for (int i = 0; i < lotteryNumbers.size(); i++) {
+            sheet.autoSizeColumn(i);
+        }
+
+        FileOutputStream fileOut = new FileOutputStream(fileName);
+        workbook.write(fileOut);
+        fileOut.close();
+        workbook.close();
+    }
+
+    public static void main(String[] args) throws IOException {
+        int lotteryQuantity = 6;
+        int lotteryMinValue = 1;
+        int lotteryMaxValue = 49;
+
+        System.out.println("欢迎来到彩票抽奖程序!");
+        System.out.printf("接下来将从 %d 到 %d 中抽取 %d 个随机数。%n", lotteryMinValue, lotteryMaxValue, lotteryQuantity);
+
+        List<Integer> lotteryNumbers = generateLotteryNumbers(lotteryQuantity, lotteryMinValue, lotteryMaxValue);
+        System.out.println("中奖号码为:" + lotteryNumbers);
+
+        String fileName = "lottery_numbers.xlsx";
+        exportToExcel(lotteryNumbers, fileName);
+        System.out.println("中奖号码已导出到文件:" + fileName);
+    }
+}

+ 119 - 0
src/main/java/com/kk/LotteryTicketGenerator.java

@@ -0,0 +1,119 @@
+package com.kk;
+
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Random;
+
+public class LotteryTicketGenerator {
+    public static void main(String[] args) {
+        int totalTickets = 100000;
+        int[] prizes = {1000000, 100000, 10000};
+        int[] prizeCounts = {1, 5, 100};
+        int[] prizeLevels = {1, 2, 3};
+
+        List<LotteryTicket> tickets = generateTickets(totalTickets, prizes, prizeCounts, prizeLevels);
+        exportToExcel(tickets, "lottery_tickets.xlsx");
+    }
+
+
+    public static List<LotteryTicket> generateTickets(int totalTickets, int[] prizes, int[] prizeCounts, int[] prizeLevels) {
+        List<LotteryTicket> tickets = new ArrayList<>();
+        Random random = new Random();
+
+        // Generate winning tickets
+        for (int i = 0; i < prizes.length; i++) {
+            for (int j = 0; j < prizeCounts[i]; j++) {
+                int luckyNumber = random.nextInt(totalTickets) + 1;
+                tickets.add(new LotteryTicket(luckyNumber, prizeLevels[i], prizes[i]));
+            }
+        }
+
+        // Generate non-winning tickets
+        while (tickets.size() < totalTickets) {
+            int luckyNumber = random.nextInt(totalTickets) + 1;
+            tickets.add(new LotteryTicket(luckyNumber, 12, 0));
+        }
+
+        // Shuffle the tickets
+        Collections.shuffle(tickets);
+        return tickets;
+    }
+
+
+// ...
+
+    public static void exportToExcel(List<LotteryTicket> tickets, String fileName) {
+        Workbook workbook = new XSSFWorkbook();
+        Sheet sheet = workbook.createSheet("Lottery Tickets");
+
+        // Create header row
+        Row headerRow = sheet.createRow(0);
+        headerRow.createCell(0).setCellValue("Ticket Number");
+        headerRow.createCell(1).setCellValue("Lucky Number");
+        headerRow.createCell(2).setCellValue("Prize Level");
+        headerRow.createCell(3).setCellValue("Prize Amount");
+
+        // Create data rows
+        for (int i = 0; i < tickets.size(); i++) {
+            LotteryTicket ticket = tickets.get(i);
+            Row row = sheet.createRow(i + 1);
+            row.createCell(0).setCellValue(i + 1);
+            row.createCell(1).setCellValue(ticket.getLuckyNumber());
+            row.createCell(2).setCellValue(ticket.getPrizeLevel());
+            row.createCell(3).setCellValue(ticket.getPrizeAmount());
+        }
+
+        // Auto size columns
+        for (int i = 0; i < 4; i++) {
+            sheet.autoSizeColumn(i);
+        }
+
+        // Write to file
+        try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
+            workbook.write(fileOut);
+        } catch (IOException e) {
+            System.out.println("An error occurred while exporting the data to Excel.");
+            e.printStackTrace();
+        } finally {
+            try {
+                workbook.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+
+    static class LotteryTicket {
+        private int luckyNumber;
+        private int prizeLevel;
+        private int prizeAmount;
+
+        public LotteryTicket(int luckyNumber, int prizeLevel, int prizeAmount) {
+            this.luckyNumber = luckyNumber;
+            this.prizeLevel = prizeLevel;
+            this.prizeAmount = prizeAmount;
+        }
+
+        public int getLuckyNumber() {
+            return luckyNumber;
+        }
+
+        public int getPrizeLevel() {
+            return prizeLevel;
+        }
+
+        public int getPrizeAmount() {
+            return prizeAmount;
+        }
+    }
+}