顺手写个抽奖小代码
package com.yst.b2b.trml.picture.schedule;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author xmyang07
* @date 2022/2/13
*/
public class Lucky {
public static void main(String[] args) {
Scanner setN = new Scanner(System.in);
System.out.println("输入抽奖的数字范围");
int n = setN.nextInt();
int[] luckyDraw = new int[n];
System.out.println("输入中奖号码数量");
int k = setN.nextInt();
int[] result = new int[k];
for(int i = 0; i< n; i++){
luckyDraw[i] = i+1;
}
for(int i=0 ;i<result.length;i++){
int rand = (int) (Math.random() * n);
result[i] = luckyDraw[rand];
//防止重复
luckyDraw[rand] = luckyDraw[n-1];
n--;
}
Arrays.sort(result);
System.out.println("号码范围");
for(int i: luckyDraw){
System.out.println(i);
}
System.out.println("中奖号码");
for(int i : result){
System.out.println(i);
}
}
}