Java利用数组随机抽取幸运观众如何实现
时间:2022-03-20 11:51:02
编写程序,事先将所有观众姓名输入数组,然后获得数组元素的总数量,最后在数组元素中随机抽取元素的下标,根据抽取的下标获得幸运观众的姓名。
思路如下:
定义输入框的按键事件,使用KeyEvent类的getKeyChar()函数判断其是否是回车字符,若不是则不作处理;
使用isEmpty()函数判断文本框中是否有字符串,如果没有字符串则不做处理;
若为合法输入则通过JTextArea类的append()方法把输入人名与回车符添加到人员列表;
使用selectAll()方法选择文本框所有字符;
定义点击“抽取”按钮时执行的函数,通过JTextArea类的getText()方法获取人员列表文本,存入字符串;
创建一个字符串型一维数组,将之前的字符串按回车符分割后存入该数组;
通过Math.random()生成随机数组索引,作为中奖者的数组下标;
定义包含格式参数的中奖信息;
通过String类的format()方法为中奖信息添加人员参数;
使用JTextArea类的setText()方法在文本域显示中奖信息;
定义点击“退出”按钮时执行的函数,使用System.exit(0)方法退出程序。
代码如下:
package cn.edu.xidian.crytoll;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class ArrayExample {
private JFrame frame;
private JTextField textField;
private JScrollPane scrollPane;
private JLabel label_1;
JTextArea textArea = new JTextArea();
private JTextArea textArea_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ArrayExample window = new ArrayExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ArrayExample() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("利用数组随机抽取幸运观众");
frame.setBounds(100, 100, 500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("\u8F93\u5165\u5728\u573A\u89C2\u4F17\u59D3\u540D\u6309\u56DE\u8F66");
label.setBounds(10, 10, 132, 15);
frame.getContentPane().add(label);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
do_textField_keyPressed(e);
}
});
textField.setBounds(10, 35, 132, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
scrollPane = new JScrollPane();
scrollPane.setBounds(10, 66, 132, 185);
frame.getContentPane().add(scrollPane);
textArea_1 = new JTextArea();
scrollPane.setViewportView(textArea_1);
label_1 = new JLabel("\u9009\u53D6\u89C2\u4F17\u4EBA\u5458\uFF1A");
label_1.setBounds(180, 10, 132, 15);
frame.getContentPane().add(label_1);
textArea.setBounds(180, 34, 214, 217);
frame.getContentPane().add(textArea);
JButton button = new JButton("\u62BD\u53D6");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setBounds(404, 187, 70, 23);
frame.getContentPane().add(button);
JButton button_1 = new JButton("\u9000\u51FA");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_1_actionPerformed(e);
}
});
button_1.setBounds(404, 228, 70, 23);
frame.getContentPane().add(button_1);
}
protected void do_textField_keyPressed(KeyEvent e) {
if (e.getKeyChar() != '\n')// 不是回车字符不做处理
return;
String name = textField.getText();
if (name.isEmpty())// 如果文本框没有字符串不做处理
return;
textArea_1.append(name + "\n");// 把输入人名与回车符添加到人员列表
textField.selectAll();// 选择文本框所有字符
}
protected void do_button_actionPerformed(ActionEvent e) {
String perstring = textArea_1.getText();// 获取人员列表文本
String[] personnelArray = perstring.split("\n{1,}");// 获取人员数组
int index = (int) (Math.random() * personnelArray.length);// 生成随机数组索引
// 定义包含格式参数的中奖信息
String formatArg = "本次抽取观众人员:\n\t%1$s\n恭喜%1$5s成为本次观众抽奖的大奖得主。"
+ "\n\n我们将为%1$5s颁发:\n\t过期的酸奶二十箱。";
// 为中奖信息添加人员参数
String info = String.format(formatArg, personnelArray[index]);
textArea.setText(info);// 在文本域显示中奖信息
}
protected void do_button_1_actionPerformed(ActionEvent e) {
System.exit(0);
}
}
效果如图所示:
标签:Java,数组,随机抽取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 注解学习笔记
2022-12-25 02:40:54
SpringBoot 配置文件加密的步骤
2023-10-23 02:55:55
支持SpEL表达式的自定义日志注解@SysLog介绍
2023-08-27 09:38:42
spring boot整合log4j2及MQ消费处理系统日志示例
2023-06-17 17:47:54
Mybatis中Mapper映射文件使用详解
2023-02-13 17:57:07
![](https://img.aspxhome.com/file/2023/4/76244_0s.png)
java实现在原有日期时间上加几个月或几天
2023-08-06 16:39:48
MyBatis Plus插件机制与执行流程原理分析详解
2021-10-09 20:53:48
![](https://img.aspxhome.com/file/2023/5/77155_0s.jpg)
java GUI编程之监听操作实例分析
2022-09-28 05:55:53
SpringBoot Nacos实现自动刷新
2023-09-16 04:17:09
Unity实现鼠标或者手指点击模型播放动画
2023-11-30 23:06:01
![](https://img.aspxhome.com/file/2023/4/72494_0s.jpg)
SpringSecurity登录使用JSON格式数据的方法
2021-09-10 21:40:40
![](https://img.aspxhome.com/file/2023/5/72355_0s.png)
Spring不能注入Static变量的原因及Spring注入静态变量
2023-11-24 04:16:36
Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 实现分库分表功能
2023-08-28 16:52:09
![](https://img.aspxhome.com/file/2023/7/58277_0s.jpg)
浅谈Unity中的Shader
2022-03-25 05:15:07
![](https://img.aspxhome.com/file/2023/1/77231_0s.jpg)
Android解决所有双击优化的问题
2023-07-27 09:07:03
![](https://img.aspxhome.com/file/2023/0/86010_0s.png)
SpringBoot中 Jackson 日期的时区和日期格式问题解决
2021-09-06 19:37:50
![](https://img.aspxhome.com/file/2023/6/67446_0s.png)
c#实现选择排序的示例
2021-10-15 00:32:27
![](https://img.aspxhome.com/file/2023/6/78116_0s.gif)
Java文件上传下载、邮件收发实例代码
2022-07-05 06:40:00
MyEclipse2018中安装Mybatis generator插件的实现步骤
2022-02-17 03:47:37
![](https://img.aspxhome.com/file/2023/9/63649_0s.png)
解析Java实现随机验证码功能的方法详解
2022-09-25 23:26:14