• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java实现抽奖功能的抽奖模板盲盒抽奖都可以套用上

武飞扬头像
小库抢板12
帮助1

entity

  1.  
    public class Goods {
  2.  
    public Goods(String name, int wight) {
  3.  
    this.name = name;
  4.  
    this.wight = wight;
  5.  
    }
  6.  
     
  7.  
    public String getName() {
  8.  
    return name;
  9.  
    }
  10.  
     
  11.  
    public void setName(String name) {
  12.  
    this.name = name;
  13.  
    }
  14.  
     
  15.  
    public int getWight() {
  16.  
    return wight;
  17.  
    }
  18.  
     
  19.  
    public void setWight(int wight) {
  20.  
    this.wight = wight;
  21.  
    }
  22.  
     
  23.  
    private String name; //商品名
  24.  
    private int wight ; //权重
  25.  
    }
学新通

VO

  1.  
    public class Container {
  2.  
    public Container(String name, int num) {
  3.  
    this.name = name;
  4.  
    this.num = num;
  5.  
    }
  6.  
     
  7.  
    public String getName() {
  8.  
    return name;
  9.  
    }
  10.  
     
  11.  
    public void setName(String name) {
  12.  
    this.name = name;
  13.  
    }
  14.  
     
  15.  
    public int getNum() {
  16.  
    return num;
  17.  
    }
  18.  
     
  19.  
    public void setNum(int num) {
  20.  
    this.num = num;
  21.  
    }
  22.  
     
  23.  
    // 容器
  24.  
    private String name;
  25.  
    private int num;//抽奖会用上
  26.  
     
  27.  
    }
学新通

实现类

  1.  
    import java.util.ArrayList;
  2.  
    import java.util.HashMap;
  3.  
    import java.util.List;
  4.  
     
  5.  
    public class Lottery {
  6.  
    public static String draw(){
  7.  
    // 列出商品
  8.  
    Goods goods1 =new Goods("洗洁精",20);
  9.  
    Goods goods2 =new Goods("大米",10);
  10.  
    Goods goods3 =new Goods("火彩盒",5);
  11.  
    Goods goods4 =new Goods("lv包包",15);
  12.  
    Goods goods5 =new Goods("macbook",22);
  13.  
    Goods goods6 =new Goods("ipadpro2020",24);
  14.  
    Goods goods7 =new Goods("火影忍者全套漫画集",4);
  15.  
    // 开始抽奖
  16.  
     
  17.  
    int sum=0;//总数
  18.  
    int n=0 ;//用于抽奖
  19.  
    int a1=0;
  20.  
    int a2=0;
  21.  
    List<Container> containers =new ArrayList<>();
  22.  
     
  23.  
    containers.add(new Container(goods1.getName(),sum =goods1.getWight()));
  24.  
    containers.add(new Container(goods2.getName(),sum =goods2.getWight()));
  25.  
    containers.add(new Container(goods3.getName(),sum =goods3.getWight()));
  26.  
    containers.add(new Container(goods4.getName(),sum =goods4.getWight()));
  27.  
    containers.add(new Container(goods5.getName(),sum =goods5.getWight()));
  28.  
    containers.add(new Container(goods6.getName(),sum =goods6.getWight()));
  29.  
    containers.add(new Container(goods7.getName(),sum =goods7.getWight()));
  30.  
     
  31.  
    int num =(int)(Math.random()*sum 1); //1到权重总和的一个随机数字
  32.  
     
  33.  
    for(int i=0;i<containers.size();i ){
  34.  
    a2=containers.get(i).getNum();
  35.  
    if(num>=a1&&num<a2){
  36.  
    return containers.get(i).getName();
  37.  
    }
  38.  
    a1=a2;
  39.  
    }
  40.  
    return containers.get(containers.size()-1).getName();
  41.  
     
  42.  
     
  43.  
     
  44.  
    }
  45.  
     
  46.  
    public static void main(String[] args) {
  47.  
    String draw = draw();
  48.  
    System.out.println(draw);
  49.  
    }
  50.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgejfie
系列文章
更多 icon
同类精品
更多 icon
继续加载