java抽獎程序
A. 用java編寫一個彩票抽獎程序
我寫給你,如果是本專業,以後要多多學習。JDK 1.6
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class LuckyDrawn {
private JFrame f = new JFrame("Lucky Drawn");
private JButton btn = new JButton("Drawn");
private JTextField result = new JTextField("Result");
private static final int MIN = 1;
private static final int MAX = 30;
private static final int RESULT_COUNT = 7;
public LuckyDrawn(){
f.add(btn);
f.add(result);
result.setEditable(false);
btn.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Random rand = new Random();
Set<Integer> set = new TreeSet<Integer>();
while(set.size() < RESULT_COUNT){
set.add(new Integer(rand.nextInt(MAX)+ 1));
}
StringBuffer sb = new StringBuffer("Lucky numbers are: ");
for (Iterator iter = set.iterator(); iter.hasNext();) {
sb.append(((Integer) iter.next()).intValue()).append(", ");
}
result.setText(sb.substring(0, sb.length() - 2).toString());
f.pack();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
f.setLayout(new GridLayout(2, 1));
f.setVisible(true);
f.setLocation(500, 200);
f.pack();
f.validate();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LuckyDrawn();
}
}
B. java抽獎程序
我給你個比較簡單的,,但是需要按照你的要求進行稍微的修改。。然後在main方法中去執行就可以了:
public class GoodLuck {
int custNo;
int i=1;
String answer;
String awardName;
public void LuckNo(){
Scanner input=new Scanner(System.in);
System.out.println("\n我行我素購物管理系統 > 幸運抽獎\n");
do{
// 需要的話請把隨機數調整成你想要的范圍(我這個是為了測試方便寫的1
(~3的隨機數,根據你的需要把下面的3換成你想要的數字就行了)
int num=(int)(Math.random()*3+1);
System.out.print("請輸入會員卡號(4位整數):");
custNo=input.nextInt();
//百位數與隨機數相同的為幸運者
int =custNo/100%10;
while(i==1){
if(custNo>=1000&&custNo<=9999){
break;
}
else{
System.out.println("\n會員號碼輸入有誤,請重新輸入:");
custNo=input.nextInt();
continue;
}
}
if(==num){
showAward();
System.out.print("\n卡號:"+custNo+"是幸運客戶,獲得"+awardName);
}else{
System.out.print("\n卡號:"+custNo+"\t謝謝您的支持!");
}
System.out.println("\n是否繼續(y/n)");
answer=input.next();
while(i==1){
if(answer.equals("y")||answer.equals("n")){
break;
}else{
System.out.print("輸入有誤!請重新輸入:");
answer=input.next();
continue;
}
}
}while(!answer.equals("n"));
}
public void showAward(){
int num=(int)(Math.random()*3+1);
if(num==1){
awardName="Mp3";
}
else if(num==2){
awardName="美的微波爐";
}
else{
awardName="美的電飯鍋";
}
}
C. 用java寫一個抽獎程序
這一百個隨機數給個范圍 random(范圍),再分批獲取唄 ,獲取到的數判斷在list里存在不,存在就重新獲取,不存在就添加到list中去
D. 如何用java寫一個抽獎程序 web類型的
問題一:前兩天剛寫的動態滾動條的廣告。可以模仿下。用循環做出n個div裡面的內容是姓名,樣式怎麼好看怎麼定。吧這些div放在大的div裡面用var timer = setInterval讓這些div滾動,速度慢了是廣告,快了就是抽獎。給一個按鈕,點擊停止滾動 clearInterval(timer). 這樣效果就好了。
這個是廣告的連接:http://..com/question/198782458298926805.html?oldq=1
E. java 製作抽獎小程序
幫你做了一個,不知是否滿意呢?
importjava.applet.Applet;
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.util.Random;
importjava.util.Vector;
{
=1L;
intw,h;
Buttonok,out,setup;
String[]msg;
TextField[]gaiLv;
TextField[]jiangPin;
intmx,ml;
intmaxNum;
Randomran;
Vector<Integer>fist;
Vector<Integer>sec;
Vector<Integer>third;
Vector<Integer>lucky;
booleanlot=false;
booleaniserr=false;
booleanissetup=false;
Stringmesg="輸入錯誤";
StringpriseMsg="繼續努力!";
publicvoidinit(){
w=400;
h=220;
mx=20;
ml=40;
ran=newRandom();
this.setSize(w,h);
this.setLayout(null);
ok=newButton("抽獎");
out=newButton("退出");
setup=newButton("確認設置");
msg=newString[4];
msg[0]="一等獎";
msg[1]="二等獎";
msg[2]="三等獎";
msg[3]="幸運獎";
gaiLv=newTextField[4];
jiangPin=newTextField[4];
for(inti=0;i<4;i++){
gaiLv[i]=newTextField("0.0"+(i+1));
this.add(gaiLv[i]);
gaiLv[i].setBounds(mx+ml,75+i*26,60,18);
jiangPin[i]=newTextField();
this.add(jiangPin[i]);
jiangPin[i].setBounds(mx+ml*3,75+i*26,80,18);
}
this.add(ok);
ok.setBounds(260,180,60,28);
ok.addActionListener(newLotButtonAction(this));
this.add(out);
out.setBounds(330,180,60,28);
out.addActionListener(newLotButtonAction(this));
this.add(setup);
setup.setBounds(110,180,80,24);
setup.addActionListener(newLotButtonAction(this));
}
publicvoidpaint(Graphicsg){
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(newColor(230,255,230));
g.fillRect(0,0,w,30);
g.setColor(Color.BLUE);
g.drawString("JAVA抽獎系統",130,20);
g.setColor(Color.ORANGE);
g.drawRect(10,40,230,170);
g.setColor(Color.BLACK);
g.drawString("設置",mx,60);
g.drawString("概率",mx+ml,60);
g.drawString("獎品",mx+ml*3,60);
for(inti=0;i<msg.length;i++){
g.setColor(newColor(255-(i*30),45,89));
g.drawString(msg[i],20,90+i*26);
}
if(lot==true){
g.setColor(newColor(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
if(priseMsg.length()<=7){
g.drawString(priseMsg,260,100);
}else{
g.drawString(priseMsg.substring(0,5),260,100);
g.drawString(priseMsg.substring(5),260,120);
}
}
if(iserr==true){
g.drawString(mesg,260,100);
}
}
publicvoidgetLucky(){
floatfiru=1;
floatsecu=1;
floatthiu=1;
floatfouu=1;
floatminu=1;
if(gaiLv[0].getText().trim().length()>1){
firu=Float.parseFloat(gaiLv[0].getText());
if(firu<=0||firu>=1){
iserr=true;
return;
}
if(firu<minu){
minu=firu;
}
}
if(gaiLv[1].getText().trim().length()>1){
secu=Float.parseFloat(gaiLv[1].getText());
if(secu<=0||secu>=1){
iserr=true;
return;
}
if(secu<minu){
minu=secu;
}
}
if(gaiLv[2].getText().trim().length()>1){
thiu=Float.parseFloat(gaiLv[2].getText());
if(thiu<=0||thiu>=1){
iserr=true;
return;
}
if(thiu<minu){
minu=thiu;
}
}
if(gaiLv[3].getText().trim().length()>1){
fouu=Float.parseFloat(gaiLv[3].getText());
if(fouu<=0||fouu>=1){
iserr=true;
return;
}
if(fouu<minu){
minu=fouu;
}
}
if(minu>=1||minu<=0){
iserr=true;
return;
}
floataNum=1/minu;
maxNum=(int)aNum;
intcount=(int)(firu/minu);
if(firu!=1){
fist=getLotteryVec(maxNum,count);
}else{
fist.removeAllElements();
}
count=(int)(secu/minu);
if(secu!=1){
sec=getLotteryVec(maxNum,count);
}else{
sec.removeAllElements();
}
count=(int)(thiu/minu);
if(thiu!=1){
third=getLotteryVec(maxNum,count);
}else{
third.removeAllElements();
}
count=(int)(fouu/minu);
if(fouu!=1){
lucky=getLotteryVec(maxNum,count);
}else{
lucky.removeAllElements();
}
issetup=true;
iserr=false;
}
protectedVector<Integer>getLotteryVec(intmaxNum,intnum){
Vector<Integer>result=newVector<Integer>();
for(inti=0;i<num;i++){
result.add(ran.nextInt(maxNum));
}
returnresult;
}
protectedintgetaNum(){
returnran.nextInt(maxNum);
}
publicintisLucky(intpNum){
for(inti=0;i<fist.size();i++){
if(fist.get(i)==pNum){
return1;
}
}
for(inti=0;i<sec.size();i++){
if(sec.get(i)==pNum){
return2;
}
}
for(inti=0;i<third.size();i++){
if(third.get(i)==pNum){
return3;
}
}
for(inti=0;i<lucky.size();i++){
if(lucky.get(i)==pNum){
return4;
}
}
return-1;
}
(ActionEvente){
StringacName=e.getActionCommand();
if(acName.equals("抽獎")){
if(issetup==false){
priseMsg="請先設置參數!";
lot=true;
repaint();
lot=false;
return;
}
lot=true;
priseMsg=getResult(getaNum());
repaint();
//lot=false;
}elseif(acName.equals("退出")){
this.setVisible(false);
this.stop();
this.destroy();
System.exit(0);
}elseif(acName.equals("確認設置")){
lot=false;
getLucky();
repaint();
}
}
publicStringgetResult(intnum){
intresu=isLucky(num);
Stringresult="";
switch(resu){
case-1:
result="繼續努力!";
break;
case1:
result="恭喜你!一等獎! 獲得"+jiangPin[0].getText();
break;
case2:
result="恭喜你!二等獎! 獲得"+jiangPin[1].getText();
break;
case3:
result="恭喜你!三等獎! 獲得"+jiangPin[2].getText();
break;
case4:
result="恭喜你!幸運獎! 獲得"+jiangPin[3].getText();
break;
default:
result="繼續努力!";
}
returnresult+num;
}
}
.awt.event.ActionListener{
Lotterysu;
publicLotButtonAction(Lotterybun){
this.su=bun;
}
@Override
publicvoidactionPerformed(ActionEvente){
su.ButtonActionPerformed(e);
}
}
F. java抽獎小程序源碼
import java.util.Scanner;
public class ChouJiang {
public static void main(String[] args) {
System.out.println("我行我素購物管理系統>幸運抽獎");
System.out.println("");
System.out.println("");
Scanner input=new Scanner(System.in);
System.out.print("請輸入四位會員號:");
int hyh=input.nextInt();
int q,b,s,g,sum;
q=hyh/1000;
b=hyh/100%10;
s=hyh/10%10;
g=hyh%10;
sum=q+b+s+g;
if(sum>=23)
{
System.out.println(hyh+"是幸運客戶,獲精美MP3一台!");
}else
{
System.out.println(hyh+"謝謝您的支持!");
}
}
}
G. 抽獎Java程序(是作業,急需
本來老早都給你弄好了,可你也不操心,一直沒有給你發;現在就這樣,需要改的話,說一下
/**
* 根據倒敘的字元串,獲取與最後一位相同的位數
* @param string -倒敘後的字元串
* @return -後幾位相同
*/
public static int getString( String string){
String string1 = string+"-";
//正則
String[] arr1 = string1.split(string1.toCharArray()[0]+"");
String string2 = string1.substring(0,arr1.length-1)+"-";
String[] arr2 = string2.split(string2.toCharArray()[0]+"");
return arr1.length==arr2.length?arr1.length-1:
getString(string.substring(0,arr1.length-1))
;
}
/**
* 後幾位相同
* @param num
* @return
*/
public static int get(Integer num,Integer o){
String string = (num+o)+"";
StringBuffer buffer = new StringBuffer(string);
string = buffer.reverse().toString();//字元串倒敘
int count = getString(string);
return count==3&&o==0?3:
count==4&&o==0?2:
count==6&&o==0?1:
count==3&&o==1?31:
count==4&&o==1?21:
count==6&&o==1?11:
count==3&&o==-1?32:
count==4&&o==-1?22:
count==6&&o==-1?12: -1;
}
public static void main(String[] args) {
Map<String,String> map = new TreeMap<>();
Set<Integer> set = new HashSet<>();
set.add(402223);
set.add(402221);
set.add(401223);
set.add(401221);
set.add(222223);
set.add(222221);
int b = 0,n=30;
while (true){
int random = (int) (Math.random()*(1000000-100000) + 100000);
set.add(random);
if (set.size() == n){
break;
}
}
for (Integer key:set){
int count = get(key,0);
//最後一個數字是0或9的話就只有一個
count = count < 0&&key%10!=9?get(key,1):count;
count = count < 0&&key%10!=1?get(key,-1):count;
String str = count>0?(
count/10==0?(
count==3?"third":count==2?"second":"first"
)
:(
"adjacent"
)
):
"blank";
//"歡迎再來";
map.put("買到的號碼:"+key, str);
}
System.out.println(map);
}
H. 抽獎程序 用JAVA做 求代碼
這里是提問和回答用的。寫的時候遇到什麼困難可以問的。用隨機數,抽中一個就不要再次抽中同樣的號碼,就可以了。
I. 用swing編寫一個java抽獎的程序,至少有5個按鈕,顯得高大上一點。
‍
importjava.awt.Color;//界面不是很高大上檔次,但是能滿足你的基本需求,希望對你有用
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
importjavax.swing.border.Border;
importjavax.swing.border.LineBorder;
publicclass抽獎游戲extendsJFrame{
static抽獎游戲frame;
public抽獎游戲(){
setLayout(newGridLayout(3,3,3,3));
Borderborder=newLineBorder(Color.BLUE,4);
JButtonl1=newJButton();
JButtonl2=newJButton();
JButtonl3=newJButton();
JButtonl4=newJButton();
JButtonl5=newJButton();
JButtonl6=newJButton();
JButtonl7=newJButton("祝你好運");
JButtonl8=newJButton("中獎規則");
JButtonl9=newJButton("再次抽獎");
l1.setBorder(border);
l2.setBorder(border);
l3.setBorder(border);
l4.setBorder(border);
l5.setBorder(border);
l6.setBorder(border);
l7.setBorder(border);
l8.setBorder(border);
l9.setBorder(border);
String[]num2={"1","2","3","4","5","6"};
l8.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
JOptionPane.showMessageDialog(null,"游戲規則: "+"特等獎: "
+"第一行或者第二行出現的數字全部相同 "
+"第一第二行出現升序的1,2,3,4,5,6或者降序的6,5,4,3,2,1 "+"一等獎 "
+"1:第一行或第二行的數字降序排列或者升序排列 "
+"2:第一行或者第二行相鄰的按鈕出現一樣的數字 ");
}
});
l9.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
l1.setText("*");
l2.setText("*");
l3.setText("*");
l4.setText("*");
l5.setText("*");
l6.setText("*");
l7.setBackground(Color.lightGray);
l7.setText("離中獎不遠了!");
}
});
l6.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
intj1=(int)(Math.random()*10)%6;
l1.setText(num2[j1]);
intj2=(int)(Math.random()*10)%6;
l2.setText(num2[j2]);
intj3=(int)(Math.random()*10)%6;
l3.setText(num2[j3]);
intj4=(int)(Math.random()*10)%6;
l4.setText(num2[j4]);
intj5=(int)(Math.random()*10)%6;
l5.setText(num2[j5]);
intj6=(int)(Math.random()*10)%6;
l6.setText(num2[j6]);
if(j1==j2&&j2==j3||j4==j5&&j5==j6||j1==1
&&j2==2&&j3==3&&j4==4&&j5==5&&j6==6
||j1==6&&j2==5&&j3==4&&j4==3&&j5==2
&&j6==1){
l7.setBackground(Color.red);
l7.setText("特等獎");
}elseif(j1>j2&&j2>j3||j3>j2&&j2>j1||j4>j5
&&j5>j6||j6>j5&&j5>j4||j1==j2
&&j5==j4||j1==j2&&j5==j6||j3==j2
&&j4==j5||j5==j6&&j2==j3){
l7.setBackground(Color.YELLOW);
l7.setText("一等獎");
}else{
l7.setBackground(Color.lightGray);
l7.setText("未中獎,加油!");
}
}
});
l5.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
intj1=(int)(Math.random()*10)%6;
l1.setText(num2[j1]);
intj2=(int)(Math.random()*10)%6;
l2.setText(num2[j2]);
intj3=(int)(Math.random()*10)%6;
l3.setText(num2[j3]);
intj4=(int)(Math.random()*10)%6;
l4.setText(num2[j4]);
intj5=(int)(Math.random()*10)%6;
l5.setText(num2[j5]);
intj6=(int)(Math.random()*10)%6;
l6.setText(num2[j6]);
if(j1==j2&&j2==j3||j4==j5&&j5==j6||j1==1
&&j2==2&&j3==3&&j4==4&&j5==5&&j6==6
||j1==6&&j2==5&&j3==4&&j4==3&&j5==2
&&j6==1){
l7.setBackground(Color.red);
l7.setText("特等獎");
}elseif(j1>j2&&j2>j3||j3>j2&&j2>j1||j4>j5
&&j5>j6||j6>j5&&j5>j4||j1==j2
&&j5==j4||j1==j2&&j5==j6||j3==j2
&&j4==j5||j5==j6&&j2==j3){
l7.setBackground(Color.YELLOW);
l7.setText("一等獎");
}else{
l7.setBackground(Color.lightGray);
l7.setText("未中獎,加油!");
}
}
});
l4.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
intj1=(int)(Math.random()*10)%6;
l1.setText(num2[j1]);
intj2=(int)(Math.random()*10)%6;
l2.setText(num2[j2]);
intj3=(int)(Math.random()*10)%6;
l3.setText(num2[j3]);
intj4=(int)(Math.random()*10)%6;
l4.setText(num2[j4]);
intj5=(int)(Math.random()*10)%6;
l5.setText(num2[j5]);
intj6=(int)(Math.random()*10)%6;
l6.setText(num2[j6]);
if(j1==j2&&j2==j3||j4==j5&&j5==j6||j1==1
&&j2==2&&j3==3&&j4==4&&j5==5&&j6==6
||j1==6&&j2==5&&j3==4&&j4==3&&j5==2
&&j6==1){
l7.setBackground(Color.red);
l7.setText("特等獎");
}elseif(j1>j2&&j2>j3||j3>j2&&j2>j1||j4>j5
&&j5>j6||j6>j5&&j5>j4||j1==j2
&&j5==j4||j1==j2&&j5==j6||j3==j2
&&j4==j5||j5==j6&&j2==j3){
l7.setBackground(Color.YELLOW);
l7.setText("一等獎");
}else{
l7.setBackground(Color.lightGray);
l7.setText("未中獎,加油!");
}
}
});
l3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
intj1=(int)(Math.random()*10)%6;
l1.setText(num2[j1]);
intj2=(int)(Math.random()*10)%6;
l2.setText(num2[j2]);
intj3=(int)(Math.random()*10)%6;
l3.setText(num2[j3]);
intj4=(int)(Math.random()*10)%6;
l4.setText(num2[j4]);
intj5=(int)(Math.random()*10)%6;
l5.setText(num2[j5]);
intj6=(int)(Math.random()*10)%6;
l6.setText(num2[j6]);
if(j1==j2&&j2==j3||j4==j5&&j5==j6||j1==1
&&j2==2&&j3==3&&j4==4&&j5==5&&j6==6
||j1==6&&j2==5&&j3==4&&j4==3&&j5==2
&&j6==1){
l7.setBackground(Color.red);
l7.setText("特等獎");
}elseif(j1>j2&&j2>j3||j3>j2&&j2>j1||j4>j5
&&j5>j6||j6>j5&&j5>j4||j1==j2
&&j5==j4||j1==j2&&j5==j6||j3==j2
&&j4==j5||j5==j6&&j2==j3){
l7.setBackground(Color.YELLOW);
l7.setText("一等獎");
}else{
l7.setBackground(Color.lightGray);
l7.setText("未中獎,加油!");
}
}
});
l2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
intj1=(int)(Math.random()*10)%6;
l1.setText(num2[j1]);
intj2=(int)(Math.random()*10)%6;
l2.setText(num2[j2]);
intj3=(int)(Math.random()*10)%6;
l3.setText(num2[j3]);
intj4=(int)(Math.random()*10)%6;
l4.setText(num2[j4]);
intj5=(int)(Math.random()*10)%6;
l5.setText(num2[j5]);
intj6=(int)(Math.random()*10)%6;
l6.setText(num2[j6]);
if(j1==j2&&j2==j3||j4==j5&&j5==j6||j1==1
&&j2==2&&j3==3&&j4==4&&j5==5&&j6==6
||j1==6&&j2==5&&j3==4&&j4==3&&j5==2
&&j6==1){
l7.setBackground(Color.red);
l7.setText("特等獎");
}elseif(j1>j2&&j2>j3||j3>j2&&j2>j1||j4>j5
&&j5>j6||j6>j5&&j5>j4||j1==j2
&&j5==j4||j1==j2&&j5==j6||j3==j2
&&j4==j5||j5==j6&&j2==j3){
l7.setBackground(Color.YELLOW);
l7.setText("一等獎");
}else{
l7.setBackground(Color.lightGray);
l7.setText("未中獎,加油!");
}
}
});
l1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
intj1=(int)(Math.random()*10)%6;
l1.setText(num2[j1]);
intj2=(int)(Math.random()*10)%6;
l2.setText(num2[j2]);
intj3=(int)(Math.random()*10)%6;
l3.setText(num2[j3]);
intj4=(int)(Math.random()*10)%6;
l4.setText(num2[j4]);
intj5=(int)(Math.random()*10)%6;
l5.setText(num2[j5]);
intj6=(int)(Math.random()*10)%6;
l6.setText(num2[j6]);
if(j1==j2&&j2==j3||j4==j5&&j5==j6||j1==1
&&j2==2&&j3==3&&j4==4&&j5==5&&j6==6
||j1==6&&j2==5&&j3==4&&j4==3&&j5==2
&&j6==1){
l7.setBackground(Color.red);
l7.setText("特等獎");
}elseif(j1>j2&&j2>j3||j3>j2&&j2>j1||j4>j5
&&j5>j6||j6>j5&&j5>j4||j1==j2
&&j5==j4||j1==j2&&j5==j6||j3==j2
&&j4==j5||j5==j6&&j2==j3){
l7.setBackground(Color.YELLOW);
l7.setText("一等獎");
}else{
l7.setBackground(Color.lightGray);
l7.setText("未中獎,加油!");
}
}
});
Fontfont=newFont("",Font.BOLD,20);
l1.setFont(font);
l2.setFont(font);
l3.setFont(font);
l4.setFont(font);
l5.setFont(font);
l6.setFont(font);
l7.setFont(font);
l8.setFont(font);
l9.setFont(font);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
frame=new抽獎游戲();
frame.setTitle("抽獎大戰");
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
J. 用java做一個抽獎的代碼
要在cmd下運行啊 我想你安裝JDK沒?
import java.util.*;
class Test{
private int maxSize=0;
private int selectSize=0;
private List<Integer> list=new ArrayList<Integer>();
private int[] one=new int[7];
public Test(int maxSize,int selectSize){
this.maxSize=maxSize;
this.selectSize=selectSize;
one=new int[selectSize];
for(int i=1;i<=maxSize;i++)
list.add(Integer.valueOf(i));
}
public void open(){
List<Integer> temp=new ArrayList<Integer>();
temp.addAll(list);
int[] p=new int[selectSize];
int tag=0;
for(int i=0;i<selectSize;i++){
tag=(int)(Math.random()*555555%temp.size());
p[i]=(Integer)temp.get(tag).intValue();
temp.remove(tag);
}
one=p;
}
public void paixu(){
int temp=0;
for(int i=0;i<selectSize;i++){
for(int j=0;j<selectSize-1;j++){
if(one[j]>one[j+1]) {temp=one[j];one[j]=one[j+1];one[j+1]=temp;}
}
}
}
public void printOpen(){
open();
paixu();
for(int i=0;i<one.length;i++)
System.out.print(one[i]+" ");
System.out.println();
}
static public void main(String[] str){
Test t=new Test(33,6); //彩票類型:33選6
for(int i=0;i<10;i++) //開10次獎
t.printOpen();
}
}