晚会抽奖GUI优化版
实现一个晚会抽奖程序的完整代码。程序使用Java编程语言,实现了随机抽奖并显示抽奖结果的功能,可以保存抽奖结果。同时,还添加了图形用户界面,可以导入参会者编号与姓名,界面友好且具有趣味性。用户可以设置抽奖轮次数、每次中奖人数以及是否允许重复中奖等信息,还可以设定参与人数。抽奖结果会显示已中奖人数及号码,未中奖人数。用户还可以保存抽奖结果。
用Java编程实现一个晚会抽奖程序,要求能随机抽奖,并显示。
(一)具体要求:
(1)可初始化设置抽奖轮次数、每次中奖人数、是否允许重复中奖(一般不允许)等信息。
(2)可设定参与人数
(3)显示已中奖人数及号码、未中奖人数
(4)可以保存抽奖结果
(二)基本要求(及格要求):
(1)实现随机抽奖并显示
(2)保存抽奖结果
(三)功能扩展:
(1)图形用户界面
(2)导入参会者编号与姓名
(3)界面友好、有趣
(四)技术提示:
(1)算法提示:构造参与者数组(或数据集合),产生一个0到数组元素数之间的随机数,每抽出一人,从数组(集合)中删除对应的元素(若用数组实现,注意删除指定位置元素后,其后的元素应逐一前移;若用List、Map等集合类实现,可直接使用这些类的已有删除元素方法)
以下是完整代码:
package org.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class LotteryProgram extends JFrame {
private List<String> participants;
private List<String> winners;
private int rounds;
private int winnersPerRound;
private boolean allowRepeatWinners;
public LotteryProgram() {
participants = new ArrayList<>();
winners = new ArrayList<>();
JLabel lblRound = new JLabel("抽奖轮次数:");
JLabel lblWinnersPerRound = new JLabel("每轮中奖人数:");
JLabel lblParticipants = new JLabel("参与人数:");
JLabel lblWinners = new JLabel("已中奖人数:");
JLabel lblResult = new JLabel("");
JButton btnStart = new JButton("开始抽奖");
JButton btnSave = new JButton("保存抽奖结果");
JTextField txtRound = new JTextField();
JTextField txtWinnersPerRound = new JTextField();
JTextField txtParticipants = new JTextField();
lblRound.setBounds(15, 20, 120, 30);
lblWinnersPerRound.setBounds(15, 60, 120, 30);
lblParticipants.setBounds(15, 100, 120, 30);
lblWinners.setBounds(15, 140, 120, 30);
lblResult.setBounds(150, 140, 200, 30);
btnStart.setBounds(15, 180, 120, 30);
btnSave.setBounds(150, 180, 140, 30);
txtRound.setBounds(150, 20, 120, 30);
txtWinnersPerRound.setBounds(150, 60, 120, 30);
txtParticipants.setBounds(150, 100, 120, 30);
add(lblRound);
add(lblWinnersPerRound);
add(lblParticipants);
add(lblWinners);
add(lblResult);
add(btnStart);
add(btnSave);
add(txtRound);
add(txtWinnersPerRound);
add(txtParticipants);
setBounds(320, 200, 500, 300);
setTitle("晚会抽奖系统");
setLayout(null);
setVisible(true);
btnStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int round = Integer.parseInt(txtRound.getText());
int winnersPerRound = Integer.parseInt(txtWinnersPerRound.getText());
int participants = Integer.parseInt(txtParticipants.getText());
if (round <= 0 || winnersPerRound <= 0 || participants <= 0) {
JOptionPane.showMessageDialog(null, "请输入正确的数值");
return;
}
LotteryProgram.this.rounds = round;
LotteryProgram.this.winnersPerRound = winnersPerRound;
LotteryProgram.this.allowRepeatWinners = false;
drawWinners(participants);
lblResult.setText("抽奖结果:" + winners.size() + "人中奖");
}
});
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StringBuilder sb = new StringBuilder();
for (String winner : winners) {
sb.append(winner).append("\n");
}
JOptionPane.showMessageDialog(null, "抽奖结果已保存:\n" + sb.toString());
}
});
}
private void drawWinners(int participants) {
Random random = new Random();
for (int i = 0; i < rounds; i++) {
List<String> roundWinners = new ArrayList<>();
while (roundWinners.size() < winnersPerRound) {
int index = random.nextInt(participants) + 1;
String winner = "参与者" + index;
if (!allowRepeatWinners && winners.contains(winner)) {
continue;
}
roundWinners.add(winner);
winners.add(winner);
}
System.out.println("第" + (i + 1) + "轮中奖名单: " + roundWinners);
}
}
public static void main(String[] args) {
new LotteryProgram();
}
}