zoukankan      html  css  js  c++  java
  • 第六次作业-----抽奖系统

                                           做一个抽奖程序,可以设定参与抽奖的总人数和奖项的个数,获奖不可重复。

    先奉上最简单的抽奖代码。。。

    package Choujiang;

    public class Choujiang {
        public static void main(String[]args){
            int[]a = new int[3];
            String [] person ={"张三","李四","王二","李逵","小明"};
                    
                    for(int i=0;i<3;i++){
                        a[i]=(int)(Math.random()*4+1);
                        for(int j=0;j<i;j++){
                            if(a[i]==a[j]){
                                i--;
                                break;
                            }
                        }
                    }
                    for(int i=0;i<3;i++){
                        
            System.out.println(a[i]);
            System.out.println(person[i]);
        }
    
    }
    }

    之后再补一个所学的。。。

    import java.util.*;

    public class Choujiang {
    private ArrayList List;
    private Random rand;

    public void deal(){
    //向 List容器中顺序添加指定数量的整数
    if(List == null){
    List = new ArrayList ();
    for(int i=1;i<=100;i++){
    List.add(i);
    }
    Collections.shuffle(List);
    return;
    }
    }

    public void deal(){
        //向 List容器中顺序添加指定数量的整数
        if(List == null){
            List = new ArrayList ();
            for(int i=1;i<=100;i++){
                List.add(i);
                }
            Collections.shuffle(List);
            return;
            }
        }
    
    public void draw(){
        rand = new Random();//产生随机数
        int s = rand.nextInt(List.size());//获得一等奖的号码数
        System.out.println("恭喜" + List.get(s) + "号获得一等奖!");//获得一等奖的号码数
        List.remove(s);//删除List中获得一等奖的号码
        Collections.shuffle(List);//打乱List中的号码顺序
        for(int i=0;i<2;i++){//获得二等奖的号码数
            int s2 = rand.nextInt(List.size());
            System.out.println("恭喜" + List.get(s2) + "号获得二等奖!");
            List.remove(s2);
            }
        Collections.shuffle(List);
        for(int i=0;i<3;i++){//获得三等奖的号码数
        int s3 = rand.nextInt(List.size());
        System.out.println("恭喜" + List.get(s3) + "号获得三等奖!");
        List.remove(s3);
        }
        Collections.shuffle(List);
        }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Choujiang cj = new Choujiang();
        cj.deal();
        cj.draw();
        }
    }

  • 相关阅读:
    导出表结构语句
    closeChannel: close the connection to remote address[] result: true
    spingboot使用rabbitmq
    服务器很卡问题排查
    docker-compose安装nginx
    Docker方式安装ShowDoc
    "docker build" requires exactly 1 argument
    Intellij IDEA常用快捷键介绍 Intellij IDEA快捷键大全汇总
    IDEA 2018 3.4 激活破解方法
    jpress:v3.2.5的docker-compose安装
  • 原文地址:https://www.cnblogs.com/lxwJava/p/5485934.html
Copyright © 2011-2022 走看看