java抽奖系统源码(抽奖系统代码java)
本文目录一览:
使用java语言编写一个抽奖系统
package ch07;
import javax.swing.*;
public class Test2 {
public static void main(String[] args) {
String output="";
output+="恭喜第"+(1+(int)(Math.random()*100))+"号中了一等奖";
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"号"+(int)(1+(Math.random()*100))+"号"+"中了二等奖";
for(int i=0;i3;i++){
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"号中了三等奖";
}
JOptionPane.showMessageDialog(null, output);
}
}
程序运行结果截图
中奖的人是随机的!
java 制作抽奖小程序
帮你做了一个,不知是否满意呢?
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.util.Vector;
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;
VectorInteger fist;
VectorInteger sec;
VectorInteger third;
VectorInteger 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();
this.setSize(w, h);
this.setLayout(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;i4;i++){
gaiLv[i]=new TextField("0.0"+(i+1));
this.add(gaiLv[i]);
gaiLv[i].setBounds(mx+ml, 75+i*26, 60, 18);
jiangPin[i]=new TextField();
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(new LotButtonAction(this));
this.add(out);
out.setBounds(330, 180, 60, 28);
out.addActionListener(new LotButtonAction(this));
this.add(setup);
setup.setBounds(110, 180, 80, 24);
setup.addActionListener(new LotButtonAction(this));
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(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(int i=0;imsg.length;i++){
g.setColor(new Color(255-(i*30),45,89));
g.drawString(msg[i], 20, 90+i*26);
}
if(lot==true){
g.setColor(new Color(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);
}
}
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=Float.parseFloat(gaiLv[0].getText());
if(firu=0||firu=1){
iserr=true;
return;
}
if(firuminu){
minu=firu;
}
}
if(gaiLv[1].getText().trim().length()1){
secu=Float.parseFloat(gaiLv[1].getText());
if(secu=0||secu=1){
iserr=true;
return;
}
if(secuminu){
minu=secu;
}
}
if(gaiLv[2].getText().trim().length()1){
thiu=Float.parseFloat(gaiLv[2].getText());
if(thiu=0||thiu=1){
iserr=true;
return;
}
if(thiuminu){
minu=thiu;
}
}
if(gaiLv[3].getText().trim().length()1){
fouu=Float.parseFloat(gaiLv[3].getText());
if(fouu=0||fouu=1){
iserr=true;
return;
}
if(fouuminu){
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{
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;
}
protected VectorInteger getLotteryVec(int maxNum,int num){
VectorInteger result=new VectorInteger();
for(int i=0;inum;i++){
result.add(ran.nextInt(maxNum));
}
return result;
}
protected int getaNum(){
return ran.nextInt(maxNum);
}
public int isLucky(int pNum){
for(int i=0;ifist.size();i++){
if(fist.get(i)==pNum){
return 1;
}
}
for(int i=0;isec.size();i++){
if(sec.get(i)==pNum){
return 2;
}
}
for(int i=0;ithird.size();i++){
if(third.get(i)==pNum){
return 3;
}
}
for(int i=0;ilucky.size();i++){
if(lucky.get(i)==pNum){
return 4;
}
}
return -1;
}
public void ButtonActionPerformed(ActionEvent e){
String acName=e.getActionCommand();
if(acName.equals("抽奖")){
if(issetup==false){
priseMsg="请先设置参数!";
lot=true;
repaint();
lot=false;
return;
}
lot=true;
priseMsg=getResult(getaNum());
repaint();
// lot=false;
}else if(acName.equals("退出")){
this.setVisible(false);
this.stop();
this.destroy();
System.exit(0);
}else if(acName.equals("确认设置")){
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 java.awt.event.ActionListener{
Lottery su;
public LotButtonAction(Lottery bun){
this.su=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
su.ButtonActionPerformed(e);
}
}
java抽奖小程序源码
import java.util.Scanner;
public class ChouJiang {
public static void main(String[] args) {
System.out.println("java抽奖系统源码我行我素购物管理系统幸运抽奖");
System.out.println("");
System.out.println("");
Scanner input=new Scanner(System.in);
System.out.print("请输入四位会员号java抽奖系统源码:");
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+"是幸运客户java抽奖系统源码,获精美MP3一台java抽奖系统源码!");
}else
{
System.out.println(hyh+"谢谢您的支持java抽奖系统源码!");
}
}
}
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 bai=custNo/100%10;
while(i==1){
if(custNo=1000custNo=9999){
break;
}
else{
System.out.println("\n会员号码输入有误,请重新输入:");
custNo=input.nextInt();
continue;
}
}
if(bai==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="美的电饭锅";
}
}