Table of Contents
1. Title
2. Problem-solving ideas
3. Detailed code explanation
Home Java javaTutorial How to implement a lottery number generator based on Java

How to implement a lottery number generator based on Java

Apr 18, 2023 pm 05:28 PM
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 < times; i++) { List startList = getStartNumber();// 获得前段号码的集合 List endList = getEndNumber();// 获得后段号码的集合 for (int m = 0; m < startList.size(); m++) { sb.append(startList.get(m));// 在字符串生成器中添加前段号码 } sb.append(" "); for (int n = 0; n < endList.size(); n++) { sb.append(endList.get(n));// 在字符串生成器中添加后段号码 } sb.append("\n"); } textArea.setText(sb.toString());// 在文本域中显示号码 } }); button.setFont(new Font("微软雅黑", Font.PLAIN, 18)); buttonPanel.add(button); JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); textArea = new JTextArea(); textArea.setFont(new Font("微软雅黑", Font.PLAIN, 18)); scrollPane.setViewportView(textArea); } /** * 随机生成前段5个号码的方法 * * @return */ public List getStartNumber() { List list = new ArrayList(); // 创建前段号码集合 String luckyNumber = ""; for (int i = 1; i < 36; i++) { // 初始化前段号码集合 if (i < 10) { list.add("0" + i + " ");// 添加0~9的号码 } else { list.add("" + i + " ");// 添加大于9的号码 } } int roundIndex = 0; List luckylist = new ArrayList(); // 保存前段号码的List集合 for (int j = 0; j < 5; j++) { int amount = list.size(); // 获取前段号码的个数 Random r = new Random(); // 创建并实例化Random的对象 roundIndex = r.nextInt(amount); // 获取一个0到amount-1的随机数 luckyNumber = list.get(roundIndex); // 获取幸运数字 luckylist.add(luckyNumber); // 添加luckylist中 list.remove(roundIndex); // 移除刚刚产生的号码 } Collections.sort(luckylist); // 对前段号码进行排序 return luckylist; } /** * 随机生成后段2个号码的方法 * * @return */ public List getEndNumber() { List list = new ArrayList(); // 创建后段号码集合 String luckyNumber = ""; for (int i = 1; i < 13; i++) { // 初始化后段号码集合 if (i < 10) { list.add("0" + i + " ");// 添加0~9的号码 } else { list.add("" + i + " ");// 添加大于9的号码 } } int roundIndex = 0; List luckylist = new ArrayList(); // 保存后段号码的List集合 for (int j = 0; j < 2; j++) { int amount = list.size(); // 获取后段号码的个数 Random r = new Random(); // 创建并实例化Random的对象 roundIndex = r.nextInt(amount); // 获取一个0到amount-1的随机数 luckyNumber = list.get(roundIndex); // 获取幸运数字 luckylist.add(luckyNumber); // 添加luckylist中 list.remove(roundIndex); // 移除刚刚产生的号码 } Collections.sort(luckylist); // 对后段号码进行排序 return luckylist; } }
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles