用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:

用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:,第1张

生成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 javautilRandom;

public class Test {

public static void main(String[] args) {

int stuNums=30;

int[] nums=new int[stuNums];//存储学号的数组

boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了

for (int i = 0; i < stuNums; i++) {

nums[i]=i+1;//给学号赋值

}

Random r=new Random();

while(true){

int index = rnextInt(stuNums);

if(!flags[index]){

Systemoutprintln("A等:"+nums[index]);

flags[index]=true; //标记已经被使用过了

break;

}

}

for (int i = 0; i < 2; i++) {

int index = rnextInt(stuNums);

if(!flags[index]){

Systemoutprintln("B等:"+nums[index]);

flags[index]=true;

}else{

i--;//如果已经被抽取过了 ,那么i建议,再次循环

}

}

for (int i = 0; i < 3; i++) {

int index = rnextInt(stuNums);

if(!flags[index]){

Systemoutprintln("c等:"+nums[index]);

flags[index]=true;

}else{

i--;

}

}

}

}

集合法

import javautilArrayList;

import javautilCollections;

public class Test2 {

public static void main(String[] args) {

int stuNums=20;

ArrayList<Integer> list=new ArrayList<Integer>();

for (int i = 0; i < stuNums; i++) {

listadd(i+1);

}

Systemoutprintln("有序"+list);

Collectionsshuffle(list);//打乱顺序

Systemoutprintln("乱序"+list);

Systemoutprintln("A等"+listget(0));

Systemoutprintln("B等"+listget(1));

Systemoutprintln("B等"+listget(2));

Systemoutprintln("C等"+listget(3));

Systemoutprintln("C等"+listget(4));

Systemoutprintln("C等"+listget(5));

}

}

import javautilScanner;

public class ChouJiang {

public static void main(String[] args) {

Systemoutprintln("我行我素购物管理系统>幸运抽奖");

Systemoutprintln("");

Systemoutprintln("");

Scanner input=new Scanner(Systemin);

Systemoutprint("请输入四位会员号:");

int hyh=inputnextInt();

int q,b,s,g,sum;

q=hyh/1000;

b=hyh/100%10;

s=hyh/10%10;

g=hyh%10;

sum=q+b+s+g;

if(sum>=23)

{

Systemoutprintln(hyh+"是幸运客户,获精美MP3一台!");

}else

{

Systemoutprintln(hyh+"谢谢您的支持!");

}

}

}

以上就是关于用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:全部的内容,包括:用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:、Java代码实现抽奖:从班级的学号中抽出一个一等奖,两个二等奖,三个三等奖、java抽奖小程序源码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10141262.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存