
我用多线程写的,你可以参考下。
系统界面图如下:(注:重置功能为清除已经中奖号码,回归1-100都可以中奖状态。)
代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SystemThreadingThread g_Thread = null;
List<int> g_list = new List<int>();
int g_Num = 0;
delegate void g_delegate();
/// <summary>
/// 开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if(g_listCount==100)
{
MessageBoxShow("所有数字已使用,请重置!");
return;
}
button3Enabled = true;
Random r = new Random();
g_delegate dele= ChangeTxt;
g_Thread = new SystemThreadingThread(() =>
{
while(thisIsDisposed==false)
{
g_Num = rNext(1, 100);
if(g_listContains(g_Num))
{
g_Num = 1;
}
while(g_listContains(g_Num))
{
g_Num++;
}
thisInvoke(dele);
SystemThreadingThreadSleep(50);
}
});
g_ThreadStart();
}
void ChangeTxt()
{
label2Text = g_NumToString();
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
//if(g_ThreadThreadState==SystemThreadingThreadStateRunning)
{
g_ThreadAbort();
g_listAdd(ConvertToInt32(label2Text));
}
button3Enabled = false;
}
/// <summary>
/// 重置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
g_listClear();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
SystemEnvironmentExit(0);
}
}
我给你个比较简单的,,但是需要按照你的要求进行稍微的修改。。然后在main方法中去执行就可以了:
public class GoodLuck {
int custNo;
int i=1;
String answer;
String awardName;
public void LuckNo(){
Scanner input=new Scanner(Systemin);
Systemoutprintln("\n我行我素购物管理系统 > 幸运抽奖\n");
do{
// 需要的话请把随机数调整成你想要的范围(我这个是为了测试方便写的1
(~3的随机数,根据你的需要把下面的3换成你想要的数字就行了)
int num=(int)(Mathrandom()3+1);
Systemoutprint("请输入会员卡号(4位整数):");
custNo=inputnextInt();
//百位数与随机数相同的为幸运者
int bai=custNo/100%10;
while(i==1){
if(custNo>=1000&&custNo<=9999){
break;
}
else{
Systemoutprintln("\n会员号码输入有误,请重新输入:");
custNo=inputnextInt();
continue;
}
}
if(bai==num){
showAward();
Systemoutprint("\n卡号:"+custNo+"是幸运客户,获得"+awardName);
}else{
Systemoutprint("\n卡号:"+custNo+"\t谢谢您的支持!");
}
Systemoutprintln("\n是否继续(y/n)");
answer=inputnext();
while(i==1){
if(answerequals("y")||answerequals("n")){
break;
}else{
Systemoutprint("输入有误!请重新输入:");
answer=inputnext();
continue;
}
}
}while(!answerequals("n"));
}
public void showAward(){
int num=(int)(Mathrandom()3+1);
if(num==1){
awardName="Mp3";
}
else if(num==2){
awardName="美的微波炉";
}
else{
awardName="美的电饭锅";
}
}
你需要再加一个初始化按钮,代码如下(commandbutton1就是你的next,commandbutton2是初始化按钮)
Dim arr(), count
Private Sub CommandButton1_Click()
If count <= 29 Then
n = Int(Rnd (UBound(arr) - LBound(arr) + 1)) + LBound(arr)
TextBox1Text = arr(n)
Cells(count + 1, 10) = arr(n) '测试代码可删除
Cells(count + 1, 11) = n '测试代码可删除
count = count + 1
arr(n) = arr(UBound(arr))
If UBound(arr) > 1 Then ReDim Preserve arr(1 To UBound(arr) - 1)
Else
MsgBox "所有人员都已抽完"
End If
End Sub
Private Sub CommandButton2_Click()
ReDim arr(1 To 30)
Randomize
TextBox1Text = ""
For i = 1 To 30
arr(i) = Cells(i, 1)
Next
MsgBox "已初始化"
count = 0
End Sub
Dim n As Integer
Dim a()
Private Sub Command1_Click()
If n = 0 Then '第一次点击时执行
m = Val(Text1)
ReDim a(1 To m)
For i = 1 To m '生成数组a,元素为1至m这m个数
a(i) = i
Next
For i = 1 To m - 1 '随机打乱数组a中元素
r = Int(Rnd m + 1)
tmp = a(i)
a(i) = a(r)
a(r) = tmp
Next
End If
n = n + 1 '计数点击次数
If n > Val(Text1) Then '次数超过指定次数,抽奖结束
MsgBox "抽奖结束!"
Exit Sub
End If
Text2 = a(n) '从数组中抽出第n个数,由于数组中是随机排列,所以相当于生成一个随机数,并且不会重复
End Sub
‍
import javaawtColor;//界面不是很高大上档次,但是能满足你的基本需求,希望对你有用import javaawtFont;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJOptionPane;
import javaxswingborderBorder;
import javaxswingborderLineBorder;
public class 抽奖游戏 extends JFrame {
static 抽奖游戏 frame;
public 抽奖游戏() {
setLayout(new GridLayout(3, 3, 3, 3));
Border border = new LineBorder(ColorBLUE, 4);
JButton l1 = new JButton();
JButton l2 = new JButton();
JButton l3 = new JButton();
JButton l4 = new JButton();
JButton l5 = new JButton();
JButton l6 = new JButton();
JButton l7 = new JButton("祝你好运");
JButton l8 = new JButton("中奖规则");
JButton l9 = new JButton("再次抽奖");
l1setBorder(border);
l2setBorder(border);
l3setBorder(border);
l4setBorder(border);
l5setBorder(border);
l6setBorder(border);
l7setBorder(border);
l8setBorder(border);
l9setBorder(border);
String[] num2 = { "1", "2", "3", "4", "5", "6" };
l8addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JOptionPaneshowMessageDialog(null, "游戏规则:\n" + "特等奖:\n"
+ "第一行或者第二行出现的数字全部相同\n"
+ "第一第二行出现升序的1,2,3,4,5,6或者降序的6,5,4,3,2,1\n" + "一等奖\n"
+ "1:第一行或第二行的数字降序排列或者升序排列\n"
+ "2:第一行或者第二行相邻的按钮出现一样的数字\n");
}
});
l9addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
l1setText("");
l2setText("");
l3setText("");
l4setText("");
l5setText("");
l6setText("");
l7setBackground(ColorlightGray);
l7setText("离中奖不远了!");
}
});
l6addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(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) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (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) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l5addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(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) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (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) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l4addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(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) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (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) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l3addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(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) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (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) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l2addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(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) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (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) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l1addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(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) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (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) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
Font font = new Font("", FontBOLD, 20);
l1setFont(font);
l2setFont(font);
l3setFont(font);
l4setFont(font);
l5setFont(font);
l6setFont(font);
l7setFont(font);
l8setFont(font);
l9setFont(font);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
frame = new 抽奖游戏();
framesetTitle("抽奖大战");
framesetSize(500, 500);
framesetLocationRelativeTo(null);
framesetVisible(true);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
}
找了两组程序:
代码一:
import javaawt;
import javaawtevent;
import javaxswing;
class GoodLucky extends JFrame implements ActionListener
{
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean sg=false;
public GoodLucky(){
b1setActionCommand("start");
JPanel p = new JPanel();
padd(b1);
padd(b2);
b1addActionListener(this);
b2addActionListener(this);
b2setEnabled(false);
thisgetContentPane()add(tf,"North");
thisgetContentPane()add(p,"South");
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetSize(300,200);
thissetLocation(300,300);
Cursor cu = new Cursor(CursorHAND_CURSOR);
thissetCursor(cu);
thissetVisible(true);
tfsetText("祝大家鸿运!");
thisgo();
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " ";
for(int j = 1; j <= 7;j++)
{
int i = (int)(Mathrandom() 9) + 1;
if(i < 10)
{
s = s + " 0" + i;
}
else
{
s = s + " " + i;
}
}
tfsetText(s);
}
try
{
Threadsleep(100);
}
catch(javalangInterruptedException e)
{
eprintStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = egetActionCommand();
if(sequals("start"))
{
sg = true;
b1setEnabled(false);
b2setEnabled(true);
}
else
{
sg= false;
b2setEnabled(false);
b1setEnabled(true);
}
}
public static void main(String[] args)
{
new GoodLucky();
}
}
代码二:
package comsofteemlesson03;
import javaawtBorderLayout;
import javaawtContainer;
import javaawtFont;
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJTextField;
public class LuckyNumberInterface extends JFrame {
JLabel ltitle = new JLabel("欢迎使用本抽奖机", JLabelCENTER);
JLabel linput = new JLabel("幸运号码:");
JTextField input = new JTextField(20);
JButton btnStart = new JButton("开始");
JButton btnStop = new JButton("停止");
JPanel lpanel = new JPanel();
JPanel btnPanel = new JPanel();
public LuckyNumberInterface() {
super("抽奖机");
final LuckyNumber ln = new LuckyNumber(input);
lnstart();
Container container = getContentPane();
lpaneladd(linput);
lpaneladd(input);
btnPaneladd(btnStart);
btnPaneladd(btnStop);
Font font = new Font("宋体", FontBOLD, 16);
ltitlesetFont(font);
containeradd(ltitle, BorderLayoutNORTH);
containeradd(lpanel, BorderLayoutCENTER);
containeradd(btnPanel, BorderLayoutSOUTH);
btnStartaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
lnsetFlag(true);
}
});
btnStopaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
lnsetFlag(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 120);//设置窗口的显示大小
setLocation(400, 300);//设置窗口显示的位置
setVisible(true);//显示窗口
}
public static void main(String[] args) {
new LuckyNumberInterface();
}
}
package comsofteemlesson03;
import javatextDecimalFormat;
import javautilRandom;
import javaxswingJTextField;
public class LuckyNumber extends Thread {
private String luckyNumber = "";
private boolean flag = false;//开关
private JTextField input;
public LuckyNumber(JTextField input) {
thisinput = input;
}
public void setFlag(boolean flag) {
thisflag = flag;
}
@Override
public void run() {
while (true) {
if (flag) {
generatorLuckyNumber();
}
}
}
public void generatorLuckyNumber() {
String[] firstNumber = { "130", "131", "134", "137", "135", "139",
"150", "151", "155", "158", "159" };
Random rd = new Random();
int number = rdnextInt(10000);
DecimalFormat df = new DecimalFormat("0000");
String lastNumber = dfformat(number);
luckyNumber = firstNumber[rdnextInt(firstNumberlength)] + ""
+ lastNumber;
inputsetText("");
inputsetText(luckyNumber);
}
}
以上就是关于vb.net 多次滚动抽奖的程序代码全部的内容,包括:vb.net 多次滚动抽奖的程序代码、java抽奖程序、求Excel抽奖程序,有一个30人的名单,每点击一下按钮就不重复抽取并显示一个姓名,直至30人全部抽完。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)