抽獎程序做業

作一個抽獎程序,要求能夠設定參與抽獎的總人數和獎項的個數,獲獎不可重複。
import java.awt.;
import java.util.
;
import javax.swing.;
import java.awt.event.
;
public class choujiang {
JTextArea t = new JTextArea();
public static void main(String[] args) {
// TODO Auto-generated method stub
new choujiang().init();
}java

public void init() {
Panel p = new Panel();
JFrame jf = new JFrame("抽獎界面");
Box h = Box.createHorizontalBox();
Box v = Box.createVerticalBox();
JButton b = new JButton("開始");
A(b);
p.add(new JLabel("點擊抽獎"));
p.add(b);
v.add(p);
h.add(v);app

ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
sp.add(t);
h.add(sp);
jf.add(h);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);dom

jf.setBounds(400, 200, 400, 400);
}
public void A(final JButton b) {
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Random random = new Random();
Object[] values = new Object[6];
HashSet hashSet = new HashSet();ide

for(int i = 0;i < values.length;i++){
int number = random.nextInt(1000) + 1;
hashSet.add(number);
}orm

values = hashSet.toArray();blog

t.append("一等獎號碼爲"+values[0] + "\n");
t.append("二等獎號碼爲"+values[1]+" " +values[2]+"\n");
t.append("三等獎號碼爲"+values[3]+" "+values[4]+" "+values[5]+"\n");hash

}
});
}
}
程序運行效果如圖所示:
it

相關文章
相關標籤/搜索