用java编写一个抽奖的程序
时间: 2023-12-06 11:04:12 浏览: 80
好的,关于用 Java 编写抽奖程序,以下是我给您提供的代码:
import java.util.*;
public class Lottery {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入抽奖人数:");
int n = scanner.nextInt();
System.out.println("请输入奖品数:");
int m = scanner.nextInt();
List<Integer> list = new ArrayList<>();
for(int i = 1; i <= n; i++){
list.add(i);
}
Random random = new Random();
List<Integer> result = new ArrayList<>();
for(int i = 1; i <= m; i++){
int r = random.nextInt(n - i + 1) + 1;
result.add(list.get(r - 1));
list.remove(r - 1);
}
System.out.println("抽奖结果:");
for(int i = 0; i < result.size(); i++){
System.out.println("第" + (i + 1) + "个奖品的获得者是:" + result.get(i));
}
}
}
希望这段代码能够帮助您。
阅读全文
相关推荐













