生成100个对象,对象有个属性,其中10个是大奖,40个是小奖,50个是无奖。
放到一个List里。
每次抽中的步骤
1、随机生成0-List长度之间的数值 ,去取List中的相应对象,并移除这个对象。
代码如下。:
奖品对象类:
public class PrizeBean {
private String type;
public String getType() {
return eggType;
}
public void setType(String eggType) {
thiseggType = eggType;
}
}
奖品池初始化代码段:
{
List prizebeanList = new ArrayList();
for (int i = 0; i < 10; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBeansetType(“大奖“);
prizebeanListadd(prizeBean);
}
for (int i = 0; i < 40; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBeansetType(“小奖“);
prizebeanListadd(prizeBean);
}
for (int i = 0; i < 50; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBeansetType(“无奖“);
prizebeanListadd(prizeBean);
}
}
抽奖代码段:
/
奖品池已经空的,肯定返回无奖了。。。
/
if(prizebeanListsize()==0){
- 没有中奖哦,下次加油!
return;
}
/
随机生成,奖品池中奖品数量的数字。。取出奖品池中的数字。。移除记录。返回。。
/
int resultnum = (int) (Mathrandom() prizebeanListsize());
PrizeBean resultPrizeBean = prizebeanListget(resultnum);
prizebeanListremove(resultPrizeBean);
if(resultPrizeBeangetType() eqauls("大奖"){
- 恭喜,大奖!
}else if(resultPrizeBeangetType() eqauls("小奖"){
- 运气不错哦,小奖!
}else{
- 没有中奖哦,下次加油!
}
找了两组程序:
代码一:
import javaawt;
import javaawtevent;
import javaxswing;
class GoodLucky extends JFrame implements ActionListener
{
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean sg=false;
public GoodLucky(){
b1setActionCommand("start");
JPanel p = new JPanel();
padd(b1);
padd(b2);
b1addActionListener(this);
b2addActionListener(this);
b2setEnabled(false);
thisgetContentPane()add(tf,"North");
thisgetContentPane()add(p,"South");
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetSize(300,200);
thissetLocation(300,300);
Cursor cu = new Cursor(CursorHAND_CURSOR);
thissetCursor(cu);
thissetVisible(true);
tfsetText("祝大家鸿运!");
thisgo();
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " ";
for(int j = 1; j <= 7;j++)
{
int i = (int)(Mathrandom() 9) + 1;
if(i < 10)
{
s = s + " 0" + i;
}
else
{
s = s + " " + i;
}
}
tfsetText(s);
}
try
{
Threadsleep(100);
}
catch(javalangInterruptedException e)
{
eprintStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = egetActionCommand();
if(sequals("start"))
{
sg = true;
b1setEnabled(false);
b2setEnabled(true);
}
else
{
sg= false;
b2setEnabled(false);
b1setEnabled(true);
}
}
public static void main(String[] args)
{
new GoodLucky();
}
}
代码二:
package comsofteemlesson03;
import javaawtBorderLayout;
import javaawtContainer;
import javaawtFont;
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJTextField;
public class LuckyNumberInterface extends JFrame {
JLabel ltitle = new JLabel("欢迎使用本抽奖机", JLabelCENTER);
JLabel linput = new JLabel("幸运号码:");
JTextField input = new JTextField(20);
JButton btnStart = new JButton("开始");
JButton btnStop = new JButton("停止");
JPanel lpanel = new JPanel();
JPanel btnPanel = new JPanel();
public LuckyNumberInterface() {
super("抽奖机");
final LuckyNumber ln = new LuckyNumber(input);
lnstart();
Container container = getContentPane();
lpaneladd(linput);
lpaneladd(input);
btnPaneladd(btnStart);
btnPaneladd(btnStop);
Font font = new Font("宋体", FontBOLD, 16);
ltitlesetFont(font);
containeradd(ltitle, BorderLayoutNORTH);
containeradd(lpanel, BorderLayoutCENTER);
containeradd(btnPanel, BorderLayoutSOUTH);
btnStartaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
lnsetFlag(true);
}
});
btnStopaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
lnsetFlag(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 120);//设置窗口的显示大小
setLocation(400, 300);//设置窗口显示的位置
setVisible(true);//显示窗口
}
public static void main(String[] args) {
new LuckyNumberInterface();
}
}
package comsofteemlesson03;
import javatextDecimalFormat;
import javautilRandom;
import javaxswingJTextField;
public class LuckyNumber extends Thread {
private String luckyNumber = "";
private boolean flag = false;//开关
private JTextField input;
public LuckyNumber(JTextField input) {
thisinput = input;
}
public void setFlag(boolean flag) {
thisflag = flag;
}
@Override
public void run() {
while (true) {
if (flag) {
generatorLuckyNumber();
}
}
}
public void generatorLuckyNumber() {
String[] firstNumber = { "130", "131", "134", "137", "135", "139",
"150", "151", "155", "158", "159" };
Random rd = new Random();
int number = rdnextInt(10000);
DecimalFormat df = new DecimalFormat("0000");
String lastNumber = dfformat(number);
luckyNumber = firstNumber[rdnextInt(firstNumberlength)] + ""
+ lastNumber;
inputsetText("");
inputsetText(luckyNumber);
}
}
以上就是关于用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:全部的内容,包括:用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:、能帮忙用java做一个抽奖程序吗,就是简单的一,二,三等奖,特等奖。谢谢、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)