search
HomeJavajavaTutorialHow to implement a lottery number generator based on Java

1. Title

Big Lotto is a method of playing Chinese sports lottery. It is a result of careful research and extensive market research by the Sports Lottery Center of the State General Administration of Sports in order to adapt to the needs of market development and enrich the market structure of sports lottery. According to research, a new large-scale lottery method was launched nationwide on May 28, 2007. It's still running.

How to play: "Choose 5 from 35" in the front area + "Choose 2 from 12" in the back area

The basic gameplay is to select 5 non-repeating numbers from 135 random numbers, and select 2 from 112 random numbers. Do not repeat numbers. If it is exactly the same as the winning number, you win the first prize.

Implementation: Implement a big lottery number generator.

2. Problem-solving ideas

Create a class: SuperFun

Use SuperFun to inherit JFrame to build a form

The form mainly consists of three parts: the input part ;Display part;Generate number button

Generates a stream of pseudo-random numbers through instances of the Random class.

Method to randomly generate the first 5 numbers: getStartNumber()

Method to randomly generate the last 2 numbers: getEndNumber()

3. Detailed code explanation

package com.xiaoxuzhu;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
/**
 * Description: 大乐透
 *
 * @author xiaoxuzhu
 * @version 1.0
 *
 * <pre class="brush:php;toolbar:false">
 * 修改记录:
 * 修改后版本	        修改人		修改日期			修改内容
 * 2022/4/30.1	    xiaoxuzhu		2022/4/30		    Create
 * 
 * @date 2022/4/30  */ public class SuperFun extends JFrame {     /**      *      */     private static final long serialVersionUID = 6787592245621788484L;     private JPanel contentPane;     private JTextField textField;     private JTextArea textArea;     /**      * Launch the application.      */     public static void main(String[] args) {         try {             UIManager                     .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");         } catch (Throwable e) {             e.printStackTrace();         }         EventQueue.invokeLater(new Runnable() {             public void run() {                 try {                     SuperFun frame = new SuperFun();                     frame.setVisible(true);                 } catch (Exception e) {                     e.printStackTrace();                 }             }         });     }     /**      * Create the frame.      */     public SuperFun() {         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setBounds(100, 100, 450, 300);         contentPane = new JPanel();         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));         contentPane.setLayout(new BorderLayout(0, 0));         setContentPane(contentPane);         setTitle("大乐透号码生成器");         JPanel panel = new JPanel();         contentPane.add(panel, BorderLayout.NORTH);         panel.setLayout(new GridLayout(1, 2, 5, 5));         JLabel label = new JLabel("请输入号码组数:");         label.setFont(new Font("微软雅黑", Font.PLAIN, 18));         label.setHorizontalAlignment(SwingConstants.CENTER);         panel.add(label);         textField = new JTextField();         textField.setFont(new Font("微软雅黑", Font.PLAIN, 18));         panel.add(textField);         textField.setColumns(10);         JPanel buttonPanel = new JPanel();         contentPane.add(buttonPanel, BorderLayout.SOUTH);         JButton button = new JButton("生成号码");         button.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent e) {                 int times = Integer.parseInt(textField.getText());// 获得用户输入的需要生成的中奖号码个数                 // 省略提示购买数量太多的代码                 StringBuilder sb = new StringBuilder();// 创建字符串生成器对象                 for (int i = 0; i  startList = getStartNumber();// 获得前段号码的集合                     List endList = getEndNumber();// 获得后段号码的集合                     for (int m = 0; m  getStartNumber() {         List list = new ArrayList(); // 创建前段号码集合         String luckyNumber = "";         for (int i = 1; i  luckylist = new ArrayList(); // 保存前段号码的List集合         for (int j = 0; j  getEndNumber() {         List list = new ArrayList(); // 创建后段号码集合         String luckyNumber = "";         for (int i = 1; i  luckylist = new ArrayList(); // 保存后段号码的List集合         for (int j = 0; j 

How to implement a lottery number generator based on Java

The above is the detailed content of How to implement a lottery number generator based on Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor