生成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{
- 没有中奖哦,下次加油!
}
你是要JAVA可视化的 还是控制台的啊?
控制台的话 你可以用循环来取中奖号,将其存入list或set里(最好是set,因为set本来就不会重复)。如果用list存就另外写一个循环方法,来挨个判定该元素是不是已经在list中存在了,如果存在,就让选号的再加选一次,如果不重复,就放到list里去。。。。OK
抽取问题, 重点是 同一个学号不能重复被抽取
解决办法很多,
比如数组可以使用下标来标记,号码是否被使用,使用了就继续下一次抽取
也可以使用集合来抽取,把集合顺序打乱,然后随便抽几个就可以了
参考代码:数组法
import javautilRandom;public class Test {
public static void main(String[] args) {
int stuNums=30;
int[] nums=new int[stuNums];//存储学号的数组
boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了
for (int i = 0; i < stuNums; i++) {
nums[i]=i+1;//给学号赋值
}
Random r=new Random();
while(true){
int index = rnextInt(stuNums);
if(!flags[index]){
Systemoutprintln("A等:"+nums[index]);
flags[index]=true; //标记已经被使用过了
break;
}
}
for (int i = 0; i < 2; i++) {
int index = rnextInt(stuNums);
if(!flags[index]){
Systemoutprintln("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已经被抽取过了 ,那么i建议,再次循环
}
}
for (int i = 0; i < 3; i++) {
int index = rnextInt(stuNums);
if(!flags[index]){
Systemoutprintln("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}
集合法
import javautilArrayList;import javautilCollections;
public class Test2 {
public static void main(String[] args) {
int stuNums=20;
ArrayList<Integer> list=new ArrayList<Integer>();
for (int i = 0; i < stuNums; i++) {
listadd(i+1);
}
Systemoutprintln("有序"+list);
Collectionsshuffle(list);//打乱顺序
Systemoutprintln("乱序"+list);
Systemoutprintln("A等"+listget(0));
Systemoutprintln("B等"+listget(1));
Systemoutprintln("B等"+listget(2));
Systemoutprintln("C等"+listget(3));
Systemoutprintln("C等"+listget(4));
Systemoutprintln("C等"+listget(5));
}
}
‍
import javaawtColor;//界面不是很高大上档次,但是能满足你的基本需求,希望对你有用import javaawtFont;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJOptionPane;
import javaxswingborderBorder;
import javaxswingborderLineBorder;
public class 抽奖游戏 extends JFrame {
static 抽奖游戏 frame;
public 抽奖游戏() {
setLayout(new GridLayout(3, 3, 3, 3));
Border border = new LineBorder(ColorBLUE, 4);
JButton l1 = new JButton();
JButton l2 = new JButton();
JButton l3 = new JButton();
JButton l4 = new JButton();
JButton l5 = new JButton();
JButton l6 = new JButton();
JButton l7 = new JButton("祝你好运");
JButton l8 = new JButton("中奖规则");
JButton l9 = new JButton("再次抽奖");
l1setBorder(border);
l2setBorder(border);
l3setBorder(border);
l4setBorder(border);
l5setBorder(border);
l6setBorder(border);
l7setBorder(border);
l8setBorder(border);
l9setBorder(border);
String[] num2 = { "1", "2", "3", "4", "5", "6" };
l8addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JOptionPaneshowMessageDialog(null, "游戏规则:\n" + "特等奖:\n"
+ "第一行或者第二行出现的数字全部相同\n"
+ "第一第二行出现升序的1,2,3,4,5,6或者降序的6,5,4,3,2,1\n" + "一等奖\n"
+ "1:第一行或第二行的数字降序排列或者升序排列\n"
+ "2:第一行或者第二行相邻的按钮出现一样的数字\n");
}
});
l9addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
l1setText("");
l2setText("");
l3setText("");
l4setText("");
l5setText("");
l6setText("");
l7setBackground(ColorlightGray);
l7setText("离中奖不远了!");
}
});
l6addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(num2[j6]);
if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1
&& j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6
|| j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2
&& j6 == 1) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5
&& j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2
&& j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2
&& j4 == j5 || j5 == j6 && j2 == j3) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l5addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(num2[j6]);
if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1
&& j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6
|| j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2
&& j6 == 1) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5
&& j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2
&& j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2
&& j4 == j5 || j5 == j6 && j2 == j3) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l4addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(num2[j6]);
if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1
&& j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6
|| j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2
&& j6 == 1) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5
&& j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2
&& j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2
&& j4 == j5 || j5 == j6 && j2 == j3) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l3addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(num2[j6]);
if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1
&& j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6
|| j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2
&& j6 == 1) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5
&& j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2
&& j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2
&& j4 == j5 || j5 == j6 && j2 == j3) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l2addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(num2[j6]);
if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1
&& j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6
|| j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2
&& j6 == 1) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5
&& j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2
&& j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2
&& j4 == j5 || j5 == j6 && j2 == j3) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
l1addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int j1 = (int) (Mathrandom() 10) % 6;
l1setText(num2[j1]);
int j2 = (int) (Mathrandom() 10) % 6;
l2setText(num2[j2]);
int j3 = (int) (Mathrandom() 10) % 6;
l3setText(num2[j3]);
int j4 = (int) (Mathrandom() 10) % 6;
l4setText(num2[j4]);
int j5 = (int) (Mathrandom() 10) % 6;
l5setText(num2[j5]);
int j6 = (int) (Mathrandom() 10) % 6;
l6setText(num2[j6]);
if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1
&& j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6
|| j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2
&& j6 == 1) {
l7setBackground(Colorred);
l7setText("特等奖");
} else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5
&& j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2
&& j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2
&& j4 == j5 || j5 == j6 && j2 == j3) {
l7setBackground(ColorYELLOW);
l7setText("一等奖");
} else {
l7setBackground(ColorlightGray);
l7setText("未中奖,加油!");
}
}
});
Font font = new Font("", FontBOLD, 20);
l1setFont(font);
l2setFont(font);
l3setFont(font);
l4setFont(font);
l5setFont(font);
l6setFont(font);
l7setFont(font);
l8setFont(font);
l9setFont(font);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
frame = new 抽奖游戏();
framesetTitle("抽奖大战");
framesetSize(500, 500);
framesetLocationRelativeTo(null);
framesetVisible(true);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
}
找了两组程序:
代码一:
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);
}
}
以上就是关于用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:全部的内容,包括:用java完成一个抽奖的程序。 每次运行程序,都会从以下的抽奖结果中随机显示一个出来:、用java写一个抽奖程序、Java代码实现抽奖:从班级的学号中抽出一个一等奖,两个二等奖,三个三等奖等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)