模拟双色球彩票的购买、开奖和中奖流程
玩法规则:
1、投注:
- (1)双色球投注区分为红色球号码区和蓝色球号码区,
- (2)红色球号码区由1-33共三十三个号码组成,蓝色球号码区由1-16共十六个号码组成。
- (3)投注时选择6个红色号码和1个蓝色球号码组成。
2、中奖:
- (1)一等奖:投注号码与当期开奖号码全部相同(顺序不限,下同),即中奖;
- (2)二等奖:投注号码与当期开奖号码中的6个红色球号码相同,即中奖;
- (3)三等奖:投注号码与当期开奖号码中的任意5个红色球号码和1个蓝色球号码相同,即中奖;
- (4)四等奖:投注号码与当期开奖号码中的任意5个红色球号码相同,或与任意4个红色球号码和1个蓝色球号码相同,即中奖;
- (5)五等奖:投注号码与当期开奖号码中的任意4个红色球号码相同,或与任意3个红色球号码和1个蓝色球号码相同,即中奖;
- (6)六等奖:投注号码与当期开奖号码中的1个蓝色球号码相同,即中奖。
业务流程:
- 1、用户购买彩票,分为自选和机选两种
- 2、根据用户购买的号码和开奖的号码进行中奖对比
效果展示
- 主界面
- 自选界面
- 机器帮选界面
- 示例1:下三注
- 示例2:下一百注
源代码
程序核心:随机号码生成,中奖类型判断
import java.util.Arrays;
import java.util.Random;public class Num_main {public static String[] chooseNumsByMachine() {//红色球号码区由1-33共三十三个号码组成,蓝色球号码区由1-16共十六个号码组成。//因为双色球的号码都是类似于01,12之类的字符串,因此我们创建一个numsPool数组用来存储01~33的值String[] numsPool = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33"};//初始化boolean类型的数组numsPoolFlag,默认数值为falseboolean[] numsPoolFlag = new boolean[33] ;//1、进行红球的号码获取//初始化长度为6的String类型的userNums数组用来存储用户选择的红色号码String[] userNums = new String[6];Random random = new Random();//循环数组进行值的输入for(int i = 0; i<userNums.length;i++) {int redNum;while(true) {redNum = random.nextInt(33);if(numsPoolFlag[redNum]) {continue;}break;}//将选中的红色号码赋值给用户数组userNums[i] = numsPool[redNum];//将标志位更改为truenumsPoolFlag[redNum] = true;}// 使用 Arrays.sort() 对现有的六个红球号码进行排序Arrays.sort(userNums);//2、蓝色球号码区由1-16共十六个号码组成//初始化int类型的blueNum作为蓝色球的号码int blueNum = random.nextInt(16);//3、获得蓝球号码后进行红球号码和蓝球号码的拼合//复制userNums数组并将数组的长度设置为7userNums = Arrays.copyOf(userNums, 7);//将userNums数组的最后一位设置为蓝球号码userNums[userNums.length-1] = numsPool[blueNum];return userNums;}public static String isWinningNums(String[] userNums, String[] luckyNums) {/* (1)一等奖:投注号码与当期开奖号码全部相同(顺序不限,下同),即中奖;(2)二等奖:投注号码与当期开奖号码中的6个红色球号码相同,即中奖;(3)三等奖:投注号码与当期开奖号码中的任意5个红色球号码和1个蓝色球号码相同,即中奖;(4)四等奖:投注号码与当期开奖号码中的任意5个红色球号码相同,或与任意4个红色球号码和1个蓝色球号码相同,即中奖;(5)五等奖:投注号码与当期开奖号码中的任意4个红色球号码相同,或与任意3个红色球号码和1个蓝色球号码相同,即中奖;(6)六等奖:投注号码与当期开奖号码中的1个蓝色球号码相同,即中奖。*///初始化int类型的RedSameCount用来存储红球相同的数量int RedSameCount = 0;//初始化int 类型的isbluesame用来存储蓝球是否相同int isbluesame = 0;//判断相等数量for(int i = 0; i<userNums.length-1;i++) {for(int j = i+1; j<userNums.length-1;j++) {if (userNums[i].equals(luckyNums[j])) {RedSameCount++;}}}if(userNums[6].equals(luckyNums[6])) isbluesame = 1;int wintype;if(RedSameCount==6 && isbluesame==1) {wintype = 1;}else if (RedSameCount==6 && isbluesame==0) {wintype = 2;}else if(RedSameCount==5 && isbluesame==1) {wintype = 3;}else if(RedSameCount==5 || RedSameCount==4 && isbluesame==1) {wintype = 4;}else if(RedSameCount==4 || RedSameCount==3 && isbluesame==1) {wintype = 5;}else if(isbluesame==1) {wintype = 6;}else{ wintype = 0;}String[] award = {"很遗憾,您本次未能中奖!!!","一等奖,奖项是5000万元","二等奖,奖项是1000万元","三等奖,奖项是50万元","四等奖,奖项是2000元", "五等奖,奖项是100元", "六等奖,奖项是5元"};return award [wintype];}
}
彩票系统主界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class MyJFrame extends JFrame {public static void main(String[] args) throws AWTException {JFrame jf = new JFrame("彩票售卖机");jf.getContentPane().setBackground(Color.white);jf.setVisible(true);jf.setSize(300, 400);jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);Container container = jf.getContentPane();container.setLayout(null);JLabel label = new JLabel();label.setText("彩票购买系统");label.setForeground(Color.black);//设置字体颜色label.setPreferredSize(new Dimension(100, 55));//设置标签大小label.setHorizontalAlignment(JLabel.CENTER);//设置内容居中对齐label.setFont(new Font("微软雅黑", Font.BOLD, 22));//设置字体大小//label.setBorder(BorderFactory.createLineBorder(Color.black));//设置边框label.setCursor(new Cursor(Cursor.HAND_CURSOR));//设置鼠标样式label.setBounds(60, 30, 160, 60);JButton zx = new JButton("用户自选"); // 创建按钮zx.setFont(new Font("微软雅黑", Font.PLAIN, 13));//设置字体大小zx.setBounds(50, 140, 180, 60); // 按钮位置及大小zx.addActionListener(new ActionListener() { // 监听器,用于监听点击事件@Overridepublic void actionPerformed(ActionEvent e) {new JDialogZixuan().setVisible(true);}});JButton jx = new JButton("机器帮选"); // 创建按钮jx.setFont(new Font("微软雅黑", Font.PLAIN, 13));//设置字体大小jx.setBounds(50, 235, 180, 60); // 按钮位置及大小jx.addActionListener(new ActionListener() { // 监听器,用于监听点击事件@Overridepublic void actionPerformed(ActionEvent e) {new JDialogJixuan().setVisible(true);}});container.add(zx);container.add(jx);container.add(label);}
}
机选系统投彩和开奖
import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;class JDialogJixuan extends JDialog { // 继承JDialog类public JDialogJixuan() {super(new MyJFrame(), "机器帮选系统", true); // 实例化一个JDialog类对象,指定其父窗体、窗口标题和类型this.setSize(450, 250);this.setLocationRelativeTo(null);JPanel panel = new JPanel();panel.setBackground(new Color(255,255,255));
// panel.setLayout(null);JLabel label = new JLabel();label.setText("请输入你的下注数:");label.setFont(new Font("微软雅黑", Font.BOLD, 15));//设置字体大小panel.add(label);final JTextField xiajizhu = new JTextField(3);xiajizhu.setFont(new Font("宋体", Font.BOLD, 16));MatteBorder border = new MatteBorder(1, 1, 1, 1, new Color(40, 40, 40,120));xiajizhu.setBorder(border);panel.add(xiajizhu);JTextArea machnum = new JTextArea();machnum.setEditable(false);JScrollPane jScrollPane=new JScrollPane(machnum);jScrollPane.setPreferredSize(new Dimension(360,100));jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);jScrollPane.setBorder(border);JLabel label1 = new JLabel();JLabel label2 = new JLabel();JButton btn = new JButton("确认");label1.setText("您的机选号码为: ");label1.setHorizontalAlignment(JLabel.CENTER);//设置内容居中对齐label1.setFont(new Font("宋体", Font.BOLD, 16));//设置字体大小btn.setFont(new Font(null, Font.PLAIN, 14));btn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String[] luckyNums = Num_main.chooseNumsByMachine();StringBuilder sum_userNums = new StringBuilder();StringBuilder winNums= new StringBuilder();String[] userNums;int sum_winNums=0;for (int i = 1; i < Integer.parseInt(xiajizhu.getText())+1; i++) {userNums = Num_main.chooseNumsByMachine();sum_userNums.append("第").append(i).append("组:").append(" ").append(Arrays.toString(userNums)).append("\n");//进行用户双色球号码与中奖双色球号码的对比,判断用户是否中奖String award = Num_main.isWinningNums(userNums, luckyNums);if (award !="很遗憾,您本次未能中奖!!!") {winNums.append("第").append(i).append("组:").append(" ").append("恭喜您获得").append(award).append("\n");sum_winNums=sum_winNums+1;}}if (sum_winNums==0) {JOptionPane.showMessageDialog(null, "很遗憾,您本次未能中奖!!!", "开奖结果", JOptionPane.PLAIN_MESSAGE);}else{JOptionPane.showMessageDialog(null, winNums, "开奖结果", JOptionPane.PLAIN_MESSAGE);}machnum.setText(sum_userNums.toString());machnum.setFont(new Font("宋体", Font.BOLD, 16));//设置字体大小label2.setText("本次中奖号码为:" + Arrays.toString(luckyNums));label2.setHorizontalAlignment(JLabel.CENTER);//设置内容居中对齐label2.setFont(new Font("宋体", Font.BOLD, 16));//设置字体大小}});panel.add(btn);panel.add(label1);panel.add(jScrollPane);panel.add(label2);this.setContentPane(panel);}
}
自选系统的投彩与开奖
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;class JDialogZixuan extends JDialog { // 继承JDialog类public JDialogZixuan() {super(new MyJFrame(), "用户自选系统", true); // 实例化一个JDialog类对象,指定其父窗体、窗口标题和类型this.setSize(450, 160);this.setLocationRelativeTo(null);JPanel panel = new JPanel();panel.setBackground(new Color(255,255,255));String[] numsPool = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33"};String[] numsPool2 = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16",};JComboBox combo1 = new JComboBox(numsPool);JComboBox combo2 = new JComboBox(numsPool);JComboBox combo3 = new JComboBox(numsPool);JComboBox combo4 = new JComboBox(numsPool);JComboBox combo5 = new JComboBox(numsPool);JComboBox combo6 = new JComboBox(numsPool);JComboBox combo7 = new JComboBox(numsPool2);combo1.setFont(new Font(null, Font.PLAIN, 15));combo2.setFont(new Font(null, Font.PLAIN, 15));combo3.setFont(new Font(null, Font.PLAIN, 15));combo4.setFont(new Font(null, Font.PLAIN, 15));combo5.setFont(new Font(null, Font.PLAIN, 15));combo6.setFont(new Font(null, Font.PLAIN, 15));combo7.setFont(new Font(null, Font.PLAIN, 15));panel.add(combo1);panel.add(combo2);panel.add(combo3);panel.add(combo4);panel.add(combo5);panel.add(combo6);panel.add(combo7);JLabel label = new JLabel();JLabel label2 = new JLabel();// 创建一个按钮,点击后获取文本框中的文本JButton btn = new JButton("确认");btn.setFont(new Font(null, Font.PLAIN, 14));btn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {label.setText("你的自选号码为: " + combo1.getSelectedItem()+" "+ combo2.getSelectedItem()+" "+ combo3.getSelectedItem()+" "+ combo4.getSelectedItem()+" "+ combo5.getSelectedItem() +" "+ combo6.getSelectedItem()+" "+ combo7.getSelectedItem());String[] userNums = new String[7];//循环数组进行值的输入userNums[0] = numsPool[Integer.parseInt((String)combo1.getSelectedItem())];userNums[1] = numsPool[Integer.parseInt((String)combo2.getSelectedItem())];userNums[2] = numsPool[Integer.parseInt((String)combo3.getSelectedItem())];userNums[3] = numsPool[Integer.parseInt((String)combo4.getSelectedItem())];userNums[4] = numsPool[Integer.parseInt((String)combo5.getSelectedItem())];userNums[5] = numsPool[Integer.parseInt((String)combo6.getSelectedItem())];userNums[6] = numsPool[Integer.parseInt((String)combo7.getSelectedItem())];Arrays.sort(userNums);label.setHorizontalAlignment(JLabel.CENTER);//设置内容居中对齐label.setFont(new Font("微软雅黑", Font.BOLD, 18));//设置字体大小label.setCursor(new Cursor(Cursor.HAND_CURSOR));//设置鼠标样式// try { Thread.sleep ( 5000 ) ;
// } catch (InterruptedException ie){}String[] luckyNums = Num_main.chooseNumsByMachine();String lyNums = Arrays.toString(luckyNums);lyNums = lyNums.replace(",","");lyNums = lyNums.replace("[","");lyNums = lyNums.replace("]","");label2.setText("本次中奖号码为: "+ lyNums);label2.setHorizontalAlignment(JLabel.CENTER);//设置内容居中对齐label2.setFont(new Font("微软雅黑", Font.BOLD, 18));//设置字体大小label2.setCursor(new Cursor(Cursor.HAND_CURSOR));//设置鼠标样式String award = Num_main.isWinningNums(userNums, luckyNums);JOptionPane.showMessageDialog(null, "恭喜你获得"+award, "开奖结果", JOptionPane.PLAIN_MESSAGE);}});panel.add(btn);panel.add(label);panel.add(label2);this.setContentPane(panel);//}
}
程序下载:Java swing 彩票游戏https://download.csdn.net/download/m0_51499090/85761919
参考文献:用Java代码实现双色球彩票_yrxfight的博客-CSDN博客_java 双色球https://blog.csdn.net/yrxfight/article/details/119873698?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165605941716781685374908%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165605941716781685374908&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~baidu_landing_v2~default-4-119873698-null-null.nonecase&utm_term=%E5%BD%A9%E7%A5%A8java&spm=1018.2226.3001.4450