乐透抽奖模拟

编程入门 行业动态 更新时间:2024-10-14 06:17:01

<a href=https://www.elefans.com/category/jswz/34/1167549.html style=乐透抽奖模拟"/>

乐透抽奖模拟

程序演示:输入五个抽奖数(quickpick复选框为快速生成,personal为自己编辑),点击play程序开始,中奖情况统计在下方,默认每周开一次大乐透,drawings表示开奖次数。

源程序包括界面类文件和事件类文件。

界面类LottoMadness: 

import java.awt.*;
import javax.swing.*;public class LottoMadness extends JFrame {LottoEvent lotto  = new LottoEvent(this);JPanel row1 = new JPanel();ButtonGroup option = new ButtonGroup();JCheckBox quickpick = new JCheckBox("quickpick",false);JCheckBox personal = new JCheckBox("personal",true);JPanel row2 = new JPanel();JLabel numbersLabel = new JLabel("Your picks : ", JLabel.RIGHT);JTextField[] numbers = new JTextField[6];JLabel winnersLabel = new JLabel("winners: " , JLabel.RIGHT);JTextField[] winners = new JTextField[6];JPanel row3 = new JPanel();JButton stop = new JButton("stop");JButton play = new JButton("play");JButton reset = new JButton("reset");JPanel row4 = new JPanel();JLabel got3Label = new JLabel("3 of 6 :",JLabel.RIGHT);JTextField got3 = new JTextField("0");JLabel got4Label = new JLabel("4 of 6 :",JLabel.RIGHT);JTextField got4 = new JTextField("0");JLabel got5Label = new JLabel("5 of 6 :",JLabel.RIGHT);JTextField got5 = new JTextField("0");JLabel got6Label = new JLabel("6 of 6 :",JLabel.RIGHT);JTextField got6 = new JTextField("0");JLabel drawingsLabel = new JLabel("drawings :",JLabel.RIGHT);JTextField drawings = new JTextField("0");JLabel yearsLabel = new JLabel("years :",JLabel.RIGHT);JTextField years = new JTextField("0");public LottoMadness(){super("Lotto Madness");setSize(550, 260);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);GridLayout layout = new GridLayout(5,1,10,10);setLayout(layout);quickpick.addItemListener(lotto);personal.addItemListener(lotto);stop.addActionListener(lotto);play.addActionListener(lotto);reset.addActionListener(lotto);FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);option.add(quickpick);option.add(personal);row1.setLayout(layout1);row1.add(quickpick);row1.add(personal);add(row1);GridLayout layout2 = new GridLayout(2, 7 ,10, 10);row2.setLayout(layout2);row2.add(numbersLabel);for (int i =0 ;i<6; i++){numbers[i] = new JTextField();row2.add(numbers[i]);}row2.add(winnersLabel);for (int i=0; i<6; i++){winners[i]=new JTextField();winners[i].setEnabled(false);row2.add(winners[i]);}add(row2);FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);row3.setLayout(layout3);stop.setEnabled(false);row3.add(stop);row3.add(play);row3.add(reset);add(row3);GridLayout layout4 = new GridLayout(2, 3, 20, 10);row4.setLayout(layout4);row4.add(got3Label);got3.setEditable(false);row4.add(got3);row4.add(got4Label);got4.setEditable(false);row4.add(got4);row4.add(got5Label);got5.setEditable(false);row4.add(got5);row4.add(got6Label);got6.setEditable(false);row4.add(got6);row4.add(drawingsLabel);drawings.setEditable(false);row4.add(drawings);row4.add(yearsLabel);years.setEditable(false);row4.add(years);add(row4);setVisible(true);}public static void main (String[] arguments){LottoMadness frame = new LottoMadness();}}

事件类LottoEvent:

import javax.swing.*;
import java.awt.event.*;public class LottoEvent implements ItemListener, ActionListener, Runnable {LottoMadness gui;  //why no import LottoMadness;Thread playing;public LottoEvent(LottoMadness in){gui = in;}public void actionPerformed(ActionEvent event){String command = event.getActionCommand();if(command == "play"){startPlaying();}if(command == "stop"){stopPlaying();}if(command == "reset"){clearAllField();}}void startPlaying(){playing = new Thread(this);playing.start();gui.play.setEnabled(false);gui.stop.setEnabled(true);gui.reset.setEnabled(false);gui.quickpick.setEnabled(false);gui.personal.setEnabled(false);}void stopPlaying() {gui.stop.setEnabled(false);gui.play.setEnabled(true);gui.reset.setEnabled(true);gui.quickpick.setEnabled(true);gui.personal.setEnabled(true);playing = null;}void clearAllField() {for (int i = 0; i<6; i++){gui.numbers[i].setText(null);gui.winners[i].setText(null);}gui.got3.setText("0");gui.got4.setText("0");gui.got5.setText("0");gui.got6.setText("0");gui.drawings.setText("0");gui.years.setText("0");}public void itemStateChanged(ItemEvent event) {Object item = event.getItem();if(item ==gui.quickpick){for(int i=0; i<6; i++){int pick;do{pick = (int)Math.floor(Math.random()*50 + 1);}while(numberGone(pick,gui.numbers,i)); //To guarantee the numbers are different;gui.numbers[i].setText(""+ pick);}}else{for(int i = 0; i<6; i++){gui.numbers[i].setText(null);  //the differences between "" and null here;}}}void addOneToField(JTextField field){int num = Integer.parseInt("0"+field.getText());num++;field.setText(""+num);}boolean numberGone(int num, JTextField[] pastNums, int count){for(int i=0; i<count; i++){if(Integer.parseInt(pastNums[i].getText() )== num){return true;}}return false;}boolean matchedOne(JTextField win, JTextField[] allPicks){for(int i=0; i<6; i++){String winText = win.getText();if (winText.equals(allPicks[i].getText())){return true;}}return false;}public void run() {Thread thisThread = Thread.currentThread();  //why not new thread?while(playing == thisThread){addOneToField(gui.drawings);int draw = Integer.parseInt(gui.drawings.getText());float numYears = (float)draw/52;  //once a week;gui.years.setText(""+numYears);int matches = 0;for(int i= 0; i<6; i++){int ball;do{ball = (int)Math.floor(Math.random()*50 +1 );}while(numberGone(ball, gui.winners, i));gui.winners[i].setText(""+ball);  //why "". to change an Integer to string;if(matchedOne(gui.winners[i], gui.numbers)){matches++;}}switch(matches) {case 3:addOneToField(gui.got3);break;case 4:addOneToField(gui.got4);break;case 5:addOneToField(gui.got5);case 6:addOneToField(gui.got6);gui.stop.setEnabled(false);gui.play.setEnabled(true);playing = null;}try {Thread.sleep(100);}catch(InterruptedException e){}}}
}

 

 

更多推荐

乐透抽奖模拟

本文发布于:2024-03-05 19:42:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1713227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:乐透

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!