Home >Java >javaTutorial >How to implement a lottery number generator based on Java
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.
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()
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
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!