找了两组程序:
代码一:
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);
}
}
package ch07;
import javaxswing;
public class Test2 {
public static void main(String[] args) {
String output="";
output+="恭喜第"+(1+(int)(Mathrandom()100))+"号中了一等奖";
output+="\n恭喜第"+(1+(int)(Mathrandom()100))+"号"+(int)(1+(Mathrandom()100))+"号"+"中了二等奖";
for(int i=0;i<3;i++){
output+="\n恭喜第"+(1+(int)(Mathrandom()100))+"号中了三等奖";
}
JOptionPaneshowMessageDialog(null, output);
}
}
程序运行结果截图
中奖的人是随机的!
package comjavatest01;
import javautilScanner;
/
JAVA 模拟抽奖,输入4位号码,
如果这个号码的百位数能够被3整除,
你就可以再免费领取一张**票
@author Administrator
/
public class Chou {
public static void main(String[] args) {
Scanner sc = new Scanner(Systemin);
//循环,模拟一直抽奖
while(true){
Systemoutprintln("请输入4位号码:");
if (schasNext()) {
//获取号码
int number = scnextInt();
//获取百位的数字
int bai = number / 100 % 10;
//判断是否符合条件
if (bai % 3 == 0) {//能被3整除
Systemoutprintln("恭喜你领到了一张**票!");
}else{
Systemoutprintln("很遗憾,未中奖!");
}
}else{
Systemoutprintln("请正确输入!");
}
}
}
}
用一个Int[]数组记录随机到的数字,
插代码:
int[] count=new i[6];//用于接收生成的随机数
for(int i=0;i<ilength;i++)
{
Random rand = new Random(); int c = randnextInt(); //int范围类的随机数 c = randnextInt(30); //生成0-30以内的随机数 c = (int)(Mathrandom() 30); //0-30以内的随机数 count[i]=c;}
生成100个对象,对象有个属性,其中10个是大奖,40个是小奖,50个是无奖。
放到一个List里。
每次抽中的步骤
1、随机生成0-List长度之间的数值 ,去取List中的相应对象,并移除这个对象。
代码如下。:
奖品对象类:
public class PrizeBean {
private String type;
public String getType() {
return eggType;
}
public void setType(String eggType) {
thiseggType = eggType;
}
}
奖品池初始化代码段:
{
List prizebeanList = new ArrayList();
for (int i = 0; i < 10; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBeansetType(“大奖“);
prizebeanListadd(prizeBean);
}
for (int i = 0; i < 40; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBeansetType(“小奖“);
prizebeanListadd(prizeBean);
}
for (int i = 0; i < 50; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBeansetType(“无奖“);
prizebeanListadd(prizeBean);
}
}
抽奖代码段:
/
奖品池已经空的,肯定返回无奖了。。。
/
if(prizebeanListsize()==0){
- 没有中奖哦,下次加油!
return;
}
/
随机生成,奖品池中奖品数量的数字。。取出奖品池中的数字。。移除记录。返回。。
/
int resultnum = (int) (Mathrandom() prizebeanListsize());
PrizeBean resultPrizeBean = prizebeanListget(resultnum);
prizebeanListremove(resultPrizeBean);
if(resultPrizeBeangetType() eqauls("大奖"){
- 恭喜,大奖!
}else if(resultPrizeBeangetType() eqauls("小奖"){
- 运气不错哦,小奖!
}else{
- 没有中奖哦,下次加油!
}
//这个是我习惯的包名,呵呵package comjameswk;import javautilRandom;
import javautilScanner;public class guess {
public static boolean getIt() {
Scanner input = new Scanner(Systemin);
Random ra = new Random();
Systemoutprintln("请输入你的会员号:");
int a=0; //初始化会员号
a = inputnextInt();
int b = ranextInt(10); //随机数
Systemoutprintln("幸运数是:" + b);
int c = 0; //定义a的百位数
if (a <= 999 || a >= 10000) {
Systemoutprintln("请输入四位数");
getIt();
}
c = a / 100 % 10;
if (b == c) {
return true;
}
return false;
} public static void main(String[] args) {
String sure=null;
do{
Systemoutprintln("是否开始猜奖?(任意键开始,’N‘下次退出)");
sure=new Scanner(Systemin)next();
if(suretoLowerCase()equals("n")){
Systemoutprintln("您已经退出!");
return;
}else{
if (new guess()getIt()) {
Systemoutprintln("恭喜你是猜到了!");
} else {
Systemoutprintln("猜错了");
}
}
}while(1==1);
}
}
会有点漏洞,会员号输入时验证不能为其他字符没有做,不好意思
以上就是关于能帮忙用java做一个抽奖程序吗,就是简单的一,二,三等奖,特等奖。谢谢全部的内容,包括:能帮忙用java做一个抽奖程序吗,就是简单的一,二,三等奖,特等奖。谢谢、使用java语言编写一个抽奖系统、JAVA 模拟抽奖,输入4位号码,如果这个号码的百位数能够被3整除,你就可以再免费领取一张电影票等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)