首页 > 电脑 > 用Java编写一个抽奖系统

用Java编写一个抽奖系统

电脑 2023-02-18

如何用java技术实现幸运抽奖活动系统

import java.util.Scanner; public class LuckyNumber { /** * 幸运抽奖 */ public static void main(String[] args) { String answer = "y"; // 标识是否继续 String userName = ""; // 用户名 String password = ""; // 密码 int cardNumber = 0; // 卡号 boolean isRegister = false; // 标识是否注册 boolean isLogin = false; // 标识是否登录 int m

java语言实现一个摇号系统,但是可以内部设定中奖名单这个怎么实现?

1、简单控制台程序如下,如需界面需要自己加个。

package zhidao;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
/**
* @author bufei
* @datetime 2020年8月31日15:54:11
*/
public class YaoHao {
public static void main(String[] args) {
String xian = "= = = = = = = = =";
// 起点 终点 奖个数 指定的号码个数
int start = 0, end = 0, prizeNum = 0, defaNum = 0;
System.out.println(xian);
System.out.println("欢迎使用xxx 抽奖系统!");
System.out.println(xian);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入号码范围例如 1 10:");
start = scanner.nextInt();
end = scanner.nextInt();
System.out.println("请输入奖项个数:");
prizeNum = scanner.nextInt();
System.out.println("请输入指定中奖号码个数,不指定请输入0:");
defaNum = scanner.nextInt();
int[] defa = new int[defaNum];
if (defaNum != 0) {
System.out.println("请输入指定的中奖号码,空格隔开:");
for (int i = 0; i < defaNum; i++) {
defa[i] = scanner.nextInt();
}
}
System.out.println(xian);
System.out.println("本次抽奖中奖号码为:");
for (int num : randomDraw(start, end, prizeNum, defa)) {
System.out.print(num+" ");
}
}
/**
* @param start 抽奖范围起点
* @param end 抽奖号码范围终点
* @param prizeNum 中奖号码个数
* @param defa 指定中奖号码
* @return
*/
public static Set randomDraw(int start, int end, int prizeNum, int[] defa) {
Set set = new HashSet<>();
// 未指定中奖号码
if (defa.length == 0) {
//随机抽 prizeNum 个奖
while (set.size() < prizeNum) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
} else {
//指定了中奖号码
//把指定的号码加入进去
for (int num : defa) {
set.add(num);
}
//如果没有全部指定 则继续抽剩余的奖项
while (set.size() < prizeNum - defa.length) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
}
}
}

2、运行效果如图

指定了中奖号码

未指定中奖号码

未指定中奖号码

使用java语言编写一个抽奖系统

packagech07;

importjavax.swing.*;

publicclassTest2{


publicstaticvoidmain(String[]args){
Stringoutput="";

output+="恭喜第"+(1+(int)(Math.random()*100))+"号中了一等奖";
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"号"+(int)(1+(Math.random()*100))+"号"+"中了二等奖";
for(inti=0;i<3;i++){
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"号中了三等奖";
}
JOptionPane.showMessageDialog(null,output);
}

}

程序运行结果截图

中奖的人是随机的!

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换成你想要的数字就

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;

publicclassLotteryextendsApplet{

privatestaticfinallongserialVersionUID=1L;

intw,h;

Buttonok,out,setup;

String[]msg;

TextField[]gaiLv;

TextField[]jiangPin;

intmx,ml;

intmaxNum;

Randomran;

Vectorfist;

Vectorsec;

Vectorthird;

Vectorlucky;

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

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=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=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=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=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;

}

protectedVectorgetLotteryVec(intmaxNum,intnum){

Vectorresult=newVector();

for(inti=0;i

result.add(ran.nextInt(maxNum));

}

returnresult;

}

protectedintgetaNum(){

returnran.nextInt(maxNum);

}

publicintisLucky(intpNum){

for(inti=0;i

if(fist.get(i)==pNum){

return1;

}

}

for(inti=0;i

if(sec.get(i)==pNum){

return2;

}

}

for(inti=0;i

if(third.get(i)==pNum){

return3;

}

}

for(inti=0;i

if(lucky.get(i)==pNum){

return4;

}

}

return-1;

}

publicvoidButtonActionPerformed(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="恭喜你!一等奖!\n获得"+jiangPin[0].getText();

break;

case2:

result="恭喜你!二等奖!\n获得"+jiangPin[1].getText();

break;

case3:

result="恭喜你!三等奖!\n获得"+jiangPin[2].getText();

break;

case4:

result="恭喜你!幸运奖!\n获得"+jiangPin[3].getText();

break;

default:

result="继续努力!";

}

returnresult+num;

}

}

classLotButtonActionimplementsjava.awt.event.ActionListener{

Lotterysu;

publicLotButtonAction(Lotterybun){

this.su=bun;

}

@Override

publicvoidactionPerformed(ActionEvente){

su.ButtonActionPerformed(e);

}

}


标签:Java 信息技术 Java编程 编程

大明白知识网 Copyright © 2020-2022 www.wangpan131.com. Some Rights Reserved. 京ICP备11019930号-18