這篇文章給大家介紹java中怎么利用數(shù)組隨機(jī)抽取幸運(yùn)觀眾,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營(yíng)理念,通過多達(dá)十載累計(jì)超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)整合營(yíng)銷推廣解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶,其中包括:成都濕噴機(jī)等企業(yè),備受客戶夸獎(jiǎng)。
首先將所有觀眾姓名生成數(shù)組,然后獲取數(shù)組元素的總數(shù)量,再在數(shù)組元素中隨機(jī)抽取元素的下標(biāo),根據(jù)元素的下標(biāo)得到幸運(yùn)觀眾的名字。
import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextField; import javax.swing.JTextArea; import javax.swing.JScrollPane; import javax.swing.border.TitledBorder; import javax.swing.JButton; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.UIManager; import java.awt.Color; public class ArrayExample extends JFrame { private JPanel contentPane; private JTextField nameField; private JTextArea personnelArea; private JTextArea resultArea; /**@auther 葆寧 * Launch the application. */ public static void main(String[] args) { try { UIManager .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { ArrayExample frame = new ArrayExample(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ArrayExample() { setTitle("\u5229\u7528\u6570\u7EC4\u968F\u673A\u62BD\u53D6\u5E78\u8FD0\u89C2\u4F17"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 498, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel panel = new JPanel(); panel .setBorder(new TitledBorder( null, "\u8F93\u5165\u5728\u573A\u89C2\u4F17\u59D3\u540D\u6309\u56DE\u8F66", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(59, 59, 59))); panel.setBounds(10, 10, 174, 242); contentPane.add(panel); panel.setLayout(new BorderLayout(0, 5)); nameField = new JTextField(); nameField.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { do_textField_keyPressed(e); } }); panel.add(nameField, BorderLayout.NORTH); nameField.setColumns(10); JScrollPane scrollPane = new JScrollPane(); panel.add(scrollPane); personnelArea = new JTextArea(); personnelArea.setEditable(false); scrollPane.setViewportView(personnelArea); JPanel panel_1 = new JPanel(); panel_1.setBorder(new TitledBorder(null, "\u9009\u53D6\u89C2\u4F17\u4EBA\u5458\uFF1A", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(59, 59, 59))); panel_1.setBounds(183, 10, 219, 242); contentPane.add(panel_1); panel_1.setLayout(new BorderLayout(0, 0)); JScrollPane scrollPane_1 = new JScrollPane(); panel_1.add(scrollPane_1); resultArea = new JTextArea(); resultArea.setEditable(false); resultArea.setLineWrap(true); scrollPane_1.setViewportView(resultArea); JButton button = new JButton("\u62BD\u53D6"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); button.setBounds(407, 164, 63, 25); contentPane.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(407, 215, 63, 25); contentPane.add(button_1); } protected void do_textField_keyPressed(KeyEvent e) { if (e.getKeyChar() != '\n')// 不是回車字符不做處理 return; String name = nameField.getText(); if (name.isEmpty())// 如果文本框沒有字符串不做處理 return; personnelArea.append(name + "\n");// 把輸入人名與回車符添加到人員列表 nameField.selectAll();// 選擇文本框所有字符 } protected void do_button_actionPerformed(ActionEvent e) { String perstring = personnelArea.getText();// 獲取人員列表文本 String[] personnelArray = perstring.split("\n{1,}");// 獲取人員數(shù)組 int index = (int) (Math.random() * personnelArray.length);// 生成隨機(jī)數(shù)組索引 // 定義包含格式參數(shù)的中獎(jiǎng)信息 String formatArg = "本次抽取觀眾人員:\n\t%1$s\n恭喜%1$s成為本次觀眾抽獎(jiǎng)的大獎(jiǎng)得主。" + "\n\n我們將為%1$s頒發(fā):\n\t過期的酸奶二十箱。"; // 為中獎(jiǎng)信息添加人員參數(shù) String info = String.format(formatArg, personnelArray[index]); resultArea.setText(info);// 在文本域顯示中間信息 } protected void do_button_1_actionPerformed(ActionEvent e) { dispose(); } }
關(guān)于java中怎么利用數(shù)組隨機(jī)抽取幸運(yùn)觀眾就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。