78抽奖

package com.fqs.test;

import java.util.Random;
public class hello {
    public static void main(String[] args) {
        int []jang={11,22,33,44,55};
        int weishu=5;
        int index=5;
        int []arr1=getNo(weishu,index);
        //获得随机下标,打印数组
        for (int i = 0; i <weishu ; i++) {
            System.out.println("jang[]随机:"+jang[arr1[i]]);
        }




    }
    //求返回数组的方法
    public static int[] getNo(int weishu,int index) {
        int []arr=new int[weishu];
        Random r=new Random();
        arr[0]=r.nextInt(index);
        for (int i = 0; i <weishu ; ) {//拿这个数和 已经存在数组中的数 比
            int rNum=r.nextInt(index);
            boolean isFind=false;
            for (int j = 0; j <i ; j++) {//存在数组中的数
                if(arr[j]==rNum){
                    isFind=true;
                    break;

                }
            }
            if(isFind==false){
                arr[i]=rNum;
                i++;
            }

        }
        return arr;
    }


}//类结束

 

posted @ 2023-06-12 14:24  胖豆芽  阅读(2)  评论(0编辑  收藏  举报