「java抽奖机」抽奖系统代码java
今天给各位分享java抽奖机的知识,其中也会对抽奖系统代码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="美的电饭锅";
}
}
能帮忙用java做一个抽奖程序吗,就是简单的一,二,三等奖,特等奖。谢谢
找了两组程序:
代码一:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GoodLucky extends JFrame implements ActionListener
{
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean sg=false;
public GoodLucky(){
b1.setActionCommand("start");
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
b2.setEnabled(false);
this.getContentPane().add(tf,"North");
this.getContentPane().add(p,"South");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,200);
this.setLocation(300,300);
Cursor cu = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(cu);
this.setVisible(true);
tf.setText("祝大家鸿运!");
this.go();
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " ";
for(int j = 1; j = 7;j++)
{
int i = (int)(Math.random() * 9) + 1;
if(i 10)
{
s = s + " 0" + i;
}
else
{
s = s + " " + i;
}
}
tf.setText(s);
}
try
{
Thread.sleep(100);
}
catch(java.lang.InterruptedException e)
{
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("start"))
{
sg = true;
b1.setEnabled(false);
b2.setEnabled(true);
}
else
{
sg= false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public static void main(String[] args)
{
new GoodLucky();
}
}
代码二:
package com.softeem.lesson03;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LuckyNumberInterface extends JFrame {
JLabel ltitle = new JLabel("欢迎使用本抽奖机", JLabel.CENTER);
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);
ln.start();
Container container = getContentPane();
lpanel.add(linput);
lpanel.add(input);
btnPanel.add(btnStart);
btnPanel.add(btnStop);
Font font = new Font("宋体", Font.BOLD, 16);
ltitle.setFont(font);
container.add(ltitle, BorderLayout.NORTH);
container.add(lpanel, BorderLayout.CENTER);
container.add(btnPanel, BorderLayout.SOUTH);
btnStart.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(true);
}
});
btnStop.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 120);//设置窗口的显示大小
setLocation(400, 300);//设置窗口显示的位置
setVisible(true);//显示窗口
}
public static void main(String[] args) {
new LuckyNumberInterface();
}
}
package com.softeem.lesson03;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JTextField;
public class LuckyNumber extends Thread {
private String luckyNumber = "";
private boolean flag = false;//开关
private JTextField input;
public LuckyNumber(JTextField input) {
this.input = input;
}
public void setFlag(boolean flag) {
this.flag = 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 = rd.nextInt(10000);
DecimalFormat df = new DecimalFormat("0000");
String lastNumber = df.format(number);
luckyNumber = firstNumber[rd.nextInt(firstNumber.length)] + "****"
+ lastNumber;
input.setText("");
input.setText(luckyNumber);
}
}
如何用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 max =
9999;
int min =
1000;
Scanner input
= new Scanner(System.in);
do {
System.out.println("*****欢迎进入奖客富翁系统*****");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("***************************");
System.out.print("请选择菜单:");
int choice =
input.nextInt();
switch
(choice) {
case 1:
System.out.println("[奖客富翁系统
注册]");
System.out.println("请填写个人注册信息:");
System.out.print("用户名:");
userName =
input.next();
System.out.print("密码:");
password =
input.next();
//
获取4位随机数作为卡号
cardNumber =
(int)(Math.random()*(max-min))+min;
System.out.println("\n注册成功,请记好您的会员卡号");
System.out.println("用户名\t密码\t会员卡号");
System.out.println(userName
+ "\t" + password + "\t" + cardNumber);
isRegister =
true; // 注册成功,标志位设置为true
break;
case 2:
System.out.println("[奖客富翁系统
登录]");
if
(isRegister) { // 判断是否注册
//
3次输入机会
for (int i
= 1; i = 3; i++) {
System.out.print("请输入用户名:");
String
inputName = input.next();
System.out.print("请输入密码:");
String
inputPassword = input.next();
if
(userName.equals(inputName) password.equals(inputPassword)) {
System.out.println("\n欢迎您:"
+ userName);
isLogin =
true; // 登录成功,标志位设置为true
break;
} else if
(i 3) {
System.out.println("用户名或密码错误,还有"
+ (3 - i) + "次机会!");
} else
{
System.out.println("您3次均输入错误!");
}
}
} else
{
System.out.println("请先注册,再登录!");
}
break;
case 3:
System.out.println("[奖客富翁系统
抽奖]");
if
(!isLogin) { // 判断是否登录
System.out.println("请先登录,再抽奖!");
} else
{
//生成5个4位随机数字,并保存在数组中
int[]
luckynums = new int[5];
for(int i
= 0; i luckynums.length; i++){
luckynums[i] =
(int)(Math.random()*(max-min))+min;
}
System.out.print("请输入您的卡号:");
int
yourcard = input.nextInt();
int
i;
System.out.print("\n本日的幸运数字为:");
for (i = 0;
i luckynums.length; i++) {
System.out.print(luckynums[i]
+ " ");\
}
for (i = 0;
i luckynums.length; i++) {
if
(luckynums[i] == yourcard) {
System.out.println("\n恭喜!您是本日的幸运会员!");
break;
}
}
if (i ==
luckynums.length) {
System.out.println("\n抱歉!您不是本日的幸运会员!");
}
}
break;
default:
System.out.println("[您的输入有误!]");
break;
}
System.out.print("继续吗?(y/n):");
answer =
input.next();
System.out.println("");
} while
("y".equals(answer));
if
("n".equals(answer)) {
System.out.println("系统退出,谢谢使用!");
}
}
}
java抽奖机的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于抽奖系统代码java、java抽奖机的信息别忘了在本站进行查找喔。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。