Java实现抽奖功能的抽奖模板盲盒抽奖都可以套用上
entity
-
public class Goods {
-
public Goods(String name, int wight) {
-
this.name = name;
-
this.wight = wight;
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getWight() {
-
return wight;
-
}
-
-
public void setWight(int wight) {
-
this.wight = wight;
-
}
-
-
private String name; //商品名
-
private int wight ; //权重
-
}
VO
-
public class Container {
-
public Container(String name, int num) {
-
this.name = name;
-
this.num = num;
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getNum() {
-
return num;
-
}
-
-
public void setNum(int num) {
-
this.num = num;
-
}
-
-
// 容器
-
private String name;
-
private int num;//抽奖会用上
-
-
}
实现类
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
-
public class Lottery {
-
public static String draw(){
-
// 列出商品
-
Goods goods1 =new Goods("洗洁精",20);
-
Goods goods2 =new Goods("大米",10);
-
Goods goods3 =new Goods("火彩盒",5);
-
Goods goods4 =new Goods("lv包包",15);
-
Goods goods5 =new Goods("macbook",22);
-
Goods goods6 =new Goods("ipadpro2020",24);
-
Goods goods7 =new Goods("火影忍者全套漫画集",4);
-
// 开始抽奖
-
-
int sum=0;//总数
-
int n=0 ;//用于抽奖
-
int a1=0;
-
int a2=0;
-
List<Container> containers =new ArrayList<>();
-
-
containers.add(new Container(goods1.getName(),sum =goods1.getWight()));
-
containers.add(new Container(goods2.getName(),sum =goods2.getWight()));
-
containers.add(new Container(goods3.getName(),sum =goods3.getWight()));
-
containers.add(new Container(goods4.getName(),sum =goods4.getWight()));
-
containers.add(new Container(goods5.getName(),sum =goods5.getWight()));
-
containers.add(new Container(goods6.getName(),sum =goods6.getWight()));
-
containers.add(new Container(goods7.getName(),sum =goods7.getWight()));
-
-
int num =(int)(Math.random()*sum 1); //1到权重总和的一个随机数字
-
-
for(int i=0;i<containers.size();i ){
-
a2=containers.get(i).getNum();
-
if(num>=a1&&num<a2){
-
return containers.get(i).getName();
-
}
-
a1=a2;
-
}
-
return containers.get(containers.size()-1).getName();
-
-
-
-
}
-
-
public static void main(String[] args) {
-
String draw = draw();
-
System.out.println(draw);
-
}
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgejfie
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01