import java.awt.GridLayout;
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),單縣企業(yè)網(wǎng)站建設(shè),單縣品牌網(wǎng)站建設(shè),網(wǎng)站定制,單縣網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,單縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class test {
static String[] arr;
static boolean flag=true;
static ListString a;
static JTextField jtf;
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setLayout(new GridLayout(1,2));
JButton button = new JButton("抽獎(jiǎng)");
jtf = new JTextField();
frame.add(jtf);
frame.add(button);
a = new ArrayListString();
arr = new String[]{"張三","李四","王五"};
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i=0;
fond:
while(true){
String str = arr[(int)(Math.random()*arr.length)];
for(String b:a){
if(b.equals(str)){
if(i=arr.length){
jtf.setText("沒(méi)有人了");
break fond;
}
continue fond;
}
i++;
}
a.add(str);
jtf.setText(str);
break;
}
}
});
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}
最簡(jiǎn)單的一個(gè),你把所有名字存進(jìn)一個(gè)數(shù)組,下面是關(guān)鍵代碼
String[] nameList={"張三","李四","王五"......};//名單全部寫(xiě)上去就可以了
int rdmNum=(int)(Math.random()*nameList.length)//隨即生成0---“總?cè)藬?shù)-1”的數(shù)字,正好對(duì)應(yīng)數(shù)組的索引
System.out.println(nameList[ramNum]);//輸出對(duì)應(yīng)的人名
簡(jiǎn)單嗎?關(guān)鍵代碼我都給寫(xiě)出來(lái)了哦,用小軟件把它生成一個(gè)EXE文件就可以用了。
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class test_random extends JFrame implements ActionListener{
public static void main(String[] args) {
test_random t = new test_random();
t.init();
}
public void init(){
initListDate();
but = new JButton("開(kāi)始點(diǎn)名");
but.setBounds(100,150,100,40);
but.addActionListener(this);
label = new JLabel("隨機(jī)點(diǎn)名");
label.setBounds(60,20,300,40);
label.setFont(new Font("楷體",Font.BOLD,40));
show = new JLabel("");
show.setBounds(110,80,200,30);
show.setFont(new Font("楷體",Font.BOLD,30));
reset = new JButton("重新點(diǎn)名");
reset.setBounds(203,246,90,25);
reset.addActionListener(this);
add(but);
add(label);
add(show);
add(reset);
setLayout(null);
setVisible(true);
setResizable(false);
setBounds(100,100,300,300);
setTitle("點(diǎn)名");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void initListDate(){
//在這個(gè)地方添加姓名就可以了
list.add("劉天廷");
list.add("孔老二");
list.add("張三");
list.add("李四");
list.add("王五");
list.add("麻六");
list.add("馮七");
list.add("京八");
list.add("茅臺(tái)九");
}
public void actionPerformed(ActionEvent e) {
/*確保每人選一次*/
Object obj = e.getSource();
if(obj == but){
if(list.size()==0){
JOptionPane.showMessageDialog(null,"沒(méi)有人了!");
return ;
}else{
Random ran = new Random();
int num = ran.nextInt(list.size());
show.setText(list.get(num));
list.remove(num);
}
}
if(obj == reset){
show.setText("");
initListDate();
}
}
private JButton but ;
private JLabel label;
private JLabel show;
private JButton reset;
private ListString list = new ArrayListString();
}