今天为大家分享一个java语言编写的模拟双色球抽奖程序,目前系统功能已经很全面,后续会进一步完善。整个系统界面漂亮,有完整得源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步
开发环境
开发语言为Java,开发环境Eclipse或者IDEA都可以,数据为MySQL。运行主程序,或者执行打开JAR文件即可以运行本程序。
系统框架
利用JDK自带的SWING框架开发。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。
系统主要功能
双色球原理
我们会先随机生成红球号码和蓝球号码,并将这些号码存放到一个数组中。这里要求随机生成的红球号码存放到数组中时不能有重复的数字,并且要满足红球号码大于等于一并且小于等于三十三,蓝球号码大于等于一小于等于十六的生成条件。
双色球中奖规则
一等奖(6+1),所选号码与中奖号码完全相同。
二等奖(6+0),六个红色号码相同,蓝色不同。
三等奖(5+1),五个红色号码相同,蓝色不同。
四等奖(5+0、4+1),五个红色号码相同,蓝色不同,或者四个红色号码相同,蓝色相同。
五等奖(4+0、3+1),四个红色号码相同,蓝色不同,或者三个红色号码相同,蓝色相同。
六等奖(2+1、1+1、0+1),两个红色号码相同以及蓝色相同,或者一个红色号码相同以及蓝色相同,或者红色号码不相同,蓝色号码相同。
运行办法
下载源码后,导入到Eclipse或者IDEA中,然后运行Main.java,出现界面即表示程序运行成功
主要功能
双色球摇号机的主要功能,随机生成6个红球和一个蓝球,这7个号码互不相同。同时蓝球的号码<=16.屏幕在不停的滚动。用户通过停止按钮来进行停止,同时点重选按钮进行重新选择一组数字。
运行效果
关键代码
public class DoubleBall extends JFrame implements ActionListener,Runnable{
/**
*
*/
private static final long serialVersionUID = -6640921399000089279L;
private JPanel contentPane;
private JTextField redball;
private JTextField blueball;
private JButton startbutton ;
private JButton stopbutton ;
static DoubleBall frame;
private Thread t;
boolean isRun = true;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new DoubleBall();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DoubleBall() {
setTitle("双色球摇奖机");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(new Color(240, 240, 240));
contentPane.setLayout(null);//不设置布局方式
setContentPane(contentPane);
t = new Thread(this);
JLabel lblNewLabel = new JLabel("福彩双色球摇奖机");
lblNewLabel.setBounds(146, 25, 123, 16);
contentPane.add(lblNewLabel);
JLabel label = new JLabel("红球:");
label.setBounds(31, 72, 61, 16);
contentPane.add(label);
redball = new JTextField();
redball.setBounds(66, 65, 186, 28);
contentPane.add(redball);
redball.setColumns(10);
JLabel label_1 = new JLabel("蓝球:");
label_1.setBounds(258, 72, 39, 16);
contentPane.add(label_1);
blueball = new JTextField();
blueball.setBounds(293, 66, 61, 28);
contentPane.add(blueball);
blueball.setColumns(10);
startbutton= new JButton("开 始");
startbutton.setActionCommand("start");
startbutton.addActionListener(this);
startbutton.setBounds(135, 118, 117, 29);
contentPane.add(startbutton);
stopbutton = new JButton("停 止");
stopbutton.setActionCommand("stop");
stopbutton.addActionListener(this);
stopbutton.setBounds(135, 155, 117, 29);
contentPane.add(stopbutton);
JButton resetbutton = new JButton("重 选");
resetbutton.setActionCommand("reset");
resetbutton.addActionListener(this);
resetbutton.setBounds(135, 191, 117, 29);
contentPane.add(resetbutton);
}
private void getOneTicket(){
int redarray[] = new int[6];//new一个数组来存放红球
Random r = new Random();
int red_no =0;
int blue_no = r.nextInt(16)+1;//蓝球号码
int count =0;//生成个数
while(count<6){
boolean flag = true;
red_no =r.nextInt(33)+1;
for(int i=0;i<count;i++){
if(redarray[i]==red_no){
//如果已存在 跳出
flag = false;
break;
}
}
if(flag){ //如果不存在 放入红球数组
redarray[count]=red_no;
count++;
}
}
Arrays.sort(redarray);//从大到小排序
StringBuilder sb = new StringBuilder();
int temp;
String str;
for(int i=0;i<6;i++){
temp = redarray[i];
if(temp<10){//如果小于10 前面加10
str = "0"+temp;
}else{
str = temp+"";
}
if(i==0){
sb.append(str);
}else{
sb.append("-"+str);
}
}
redball.setText(sb.toString());
if(blue_no<10){//如果小于10 前面加0
str = "0"+blue_no;
}else{
str = blue_no+"";
}
blueball.setText(str);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("start")){
startbutton.setEnabled(false);
stopbutton.setEnabled(true);
if(isRun){
t.start();
}else{
isRun = true;
}
}else if(command.equals("stop")){
isRun = false;
startbutton.setEnabled(true);
stopbutton.setEnabled(false);
}else{
redball.setText("00-00-00-00-00-00");
blueball.setText("00");
isRun = false;
startbutton.setEnabled(true);
stopbutton.setEnabled(false);
JOptionPane.showMessageDialog(frame, "奖池已清空,请按开始按钮重新开始~");
}
}
@Override
public void run() {
//使用线程实现滚动
while(true){
if(isRun){
getOneTicket();
try {
Thread.sleep(100);//休眠0.1s
} catch (InterruptedException e) {
e.printStackTrace();
}//
}else{
try {
Thread.sleep(1000);//休眠1s
} catch (InterruptedException e) {
e.printStackTrace();
}//
}
}
}
}
项目总结
经过半个月的熬夜编码,我终于完成的模拟双色球摇号机系统设计与开发,达到了预期的目标。在开发的过程中过程中遇到不少困难,通过查阅相关资料,反复琢磨,请教同学,问题最终都得以解决。这个系统主要是我自己开发的,但也得到了老师和同学的很大帮助,在此对他们表示感谢。