帮你做了一个,不知是否满意呢?
import javaappletApplet;
import javaawtButton;
import javaawtColor;
import javaawtGraphics;
import javaawtTextField;
import javaawteventActionEvent;
import javautilRandom;
import javautilVector;
public class Lottery extends Applet {
private static final long serialVersionUID = 1L;
int w,h;
Button ok,out,setup;
String[] msg;
TextField[] gaiLv;
TextField[] jiangPin;
int mx,ml;
int maxNum;
Random ran;
Vector<Integer> fist;
Vector<Integer> sec;
Vector<Integer> third;
Vector<Integer> lucky;
boolean lot=false;
boolean iserr=false;
boolean issetup=false;
String mesg="输入错误";
String priseMsg="继续努力!";
public void init(){
w=400;
h=220;
mx=20;
ml=40;
ran=new Random();
thissetSize(w, h);
thissetLayout(null);
ok=new Button("抽奖");
out=new Button("退出");
setup=new Button("确认设置");
msg=new String[4];
msg[0]="一等奖";
msg[1]="二等奖";
msg[2]="三等奖";
msg[3]="幸运奖";
gaiLv=new TextField[4];
jiangPin=new TextField[4];
for(int i=0;i<4;i++){
gaiLv[i]=new TextField("00"+(i+1));
thisadd(gaiLv[i]);
gaiLv[i]setBounds(mx+ml, 75+i26, 60, 18);
jiangPin[i]=new TextField();
thisadd(jiangPin[i]);
jiangPin[i]setBounds(mx+ml3, 75+i26, 80, 18);
}
thisadd(ok);
oksetBounds(260, 180, 60, 28);
okaddActionListener(new LotButtonAction(this));
thisadd(out);
outsetBounds(330, 180, 60, 28);
outaddActionListener(new LotButtonAction(this));
thisadd(setup);
setupsetBounds(110, 180, 80, 24);
setupaddActionListener(new LotButtonAction(this));
}
public void paint(Graphics g){
gsetColor(Colorwhite);
gfillRect(0, 0, thisgetWidth(), thisgetHeight());
gsetColor(new Color(230,255,230));
gfillRect(0, 0, w, 30);
gsetColor(ColorBLUE);
gdrawString("JAVA抽奖系统", 130, 20);
gsetColor(ColorORANGE);
gdrawRect(10, 40, 230, 170);
gsetColor(ColorBLACK);
gdrawString("设置", mx, 60);
gdrawString("概率", mx+ml, 60);
gdrawString("奖品", mx+ml3, 60);
for(int i=0;i<msglength;i++){
gsetColor(new Color(255-(i30),45,89));
gdrawString(msg[i], 20, 90+i26);
}
if(lot==true){
gsetColor(new Color(rannextInt(255),rannextInt(255),rannextInt(255)));
if(priseMsglength()<=7){
gdrawString(priseMsg, 260, 100);
}else{
gdrawString(priseMsgsubstring(0, 5), 260, 100);
gdrawString(priseMsgsubstring(5), 260, 120);
}
}
if(iserr==true){
gdrawString(mesg, 260, 100);
}
}
public void getLucky(){
float firu=1;
float secu=1;
float thiu=1;
float fouu=1;
float minu=1;
if(gaiLv[0]getText()trim()length()>1){
firu=FloatparseFloat(gaiLv[0]getText());
if(firu<=0||firu>=1){
iserr=true;
return;
}
if(firu<minu){
minu=firu;
}
}
if(gaiLv[1]getText()trim()length()>1){
secu=FloatparseFloat(gaiLv[1]getText());
if(secu<=0||secu>=1){
iserr=true;
return;
}
if(secu<minu){
minu=secu;
}
}
if(gaiLv[2]getText()trim()length()>1){
thiu=FloatparseFloat(gaiLv[2]getText());
if(thiu<=0||thiu>=1){
iserr=true;
return;
}
if(thiu<minu){
minu=thiu;
}
}
if(gaiLv[3]getText()trim()length()>1){
fouu=FloatparseFloat(gaiLv[3]getText());
if(fouu<=0||fouu>=1){
iserr=true;
return;
}
if(fouu<minu){
minu=fouu;
}
}
if(minu>=1||minu<=0){
iserr=true;
return;
}
float aNum=1/minu;
maxNum=(int)aNum;
int count=(int)(firu/minu);
if(firu!=1){
fist=getLotteryVec(maxNum,count);
}else{
fistremoveAllElements();
}
count=(int)(secu/minu);
if(secu!=1){
sec=getLotteryVec(maxNum,count);
}else{
secremoveAllElements();
}
count=(int)(thiu/minu);
if(thiu!=1){
third=getLotteryVec(maxNum,count);
}else{
thirdremoveAllElements();
}
count=(int)(fouu/minu);
if(fouu!=1){
lucky=getLotteryVec(maxNum,count);
}else{
luckyremoveAllElements();
}
issetup=true;
iserr=false;
}
protected Vector<Integer> getLotteryVec(int maxNum,int num){
Vector<Integer> result=new Vector<Integer>();
for(int i=0;i<num;i++){
resultadd(rannextInt(maxNum));
}
return result;
}
protected int getaNum(){
return rannextInt(maxNum);
}
public int isLucky(int pNum){
for(int i=0;i<fistsize();i++){
if(fistget(i)==pNum){
return 1;
}
}
for(int i=0;i<secsize();i++){
if(secget(i)==pNum){
return 2;
}
}
for(int i=0;i<thirdsize();i++){
if(thirdget(i)==pNum){
return 3;
}
}
for(int i=0;i<luckysize();i++){
if(luckyget(i)==pNum){
return 4;
}
}
return -1;
}
public void ButtonActionPerformed(ActionEvent e){
String acName=egetActionCommand();
if(acNameequals("抽奖")){
if(issetup==false){
priseMsg="请先设置参数!";
lot=true;
repaint();
lot=false;
return;
}
lot=true;
priseMsg=getResult(getaNum());
repaint();
// lot=false;
}else if(acNameequals("退出")){
thissetVisible(false);
thisstop();
thisdestroy();
Systemexit(0);
}else if(acNameequals("确认设置")){
lot=false;
getLucky();
repaint();
}
}
public String getResult(int num){
int resu=isLucky(num);
String result="";
switch(resu){
case -1:
result="继续努力!";
break;
case 1:
result="恭喜你!一等奖!\n获得"+jiangPin[0]getText();
break;
case 2:
result="恭喜你!二等奖!\n获得"+jiangPin[1]getText();
break;
case 3:
result="恭喜你!三等奖!\n获得"+jiangPin[2]getText();
break;
case 4:
result="恭喜你!幸运奖!\n获得"+jiangPin[3]getText();
break;
default:
result="继续努力!";
}
return result+num;
}
}
class LotButtonAction implements javaawteventActionListener{
Lottery su;
public LotButtonAction(Lottery bun){
thissu=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
suButtonActionPerformed(e);
}
}
找了两组程序:
代码一:
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);
}
}
思路是对的,按照你前面的思路,后面只需要在页面上用JS来控制随机显示,和抽取了。
但是一般不这么做,因为你这么做不方便控制作弊,和中奖概率。还有JS控制抽奖很容易就能作弊。
我之前做过的一个抽奖项目是这样的:抽奖程序(抽奖界面)用flash来写,这样既方便作弊也能控制中奖概率,同时更安全,JAVA后台提供抽奖名单,中奖概率等数据,抽奖程序调用java的API获得抽奖数据,这里需要在后台对抽奖信息进行加密,我们使用的是base64加密(方便反向解密),然后由用户点击flash抽奖界面抽奖,中奖后,同样使用base64加密中奖信息,发送到java后台,然后保存在DB,同时后台程序发送中奖信息给用户。
七猫幸运抽奖是一款抖音APP上的一款抽奖软件,可以通过参与活动抽到非金币奖励,如虚拟饰品、实物奖品等。如果想要将这些奖品兑换成金币,可以在兑换页面中查看每个奖品对应的金币数量,并且需要注意的是每个奖品的兑换规则和时间周期可能会有所不同,建议用户在兑换之前认真阅读相关规则,并按照规定时间和步骤完成兑换程序。在 *** 作过程中如果遇到问题,可以尝试联系客服或参考相关指南进行解决。
以上就是关于java 制作抽奖小程序全部的内容,包括:java 制作抽奖小程序、能帮忙用java做一个抽奖程序吗,就是简单的一,二,三等奖,特等奖。谢谢、如何用java写一个抽奖程序 web类型的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)