可以做到!用 Random 得到随机的数,再根据你的登陆人的身份 控制随机的次数是多少!
例如:
1 - 20 为中奖, 普通就随机出 10000个数。会员的话就随机100个数就好了!
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javautilArrayList;
import javautilList;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJOptionPane;
import javaxswingJPanel;
public class Lottery extends JFrame {
private List<JButton> list = new ArrayList<JButton>();
private final static int SIZE = 9;
private String[] info = new String[SIZE];
private int index;
private boolean flag = true;
public static void main(String[] args) {new Lottery();}
public Lottery() {init();}
private void init() {
thissetTitle("随机抽检");
thissetSize(300, 300);
thisadd(createPanel());
thissetLocationRelativeTo(null);
thissetResizable(false);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetVisible(true);
}
private JPanel createPanel() {
JPanel panel = new JPanel();
panelsetLayout(new GridLayout(3, 3));
for (int i = 0; i < SIZE; i++) {
listadd(createButton());
}
for (int i = 0; i < SIZE; i++) {
paneladd(listget(i));
}
for (int i = 0; i < SIZE; i++) {
info[i] = "谢谢参与";
}return panel;
}
private JButton createButton() {
JButton button = new JButton();
buttonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (flag) {
index = ((int) (Mathrandom() 100)) % 9;
info[index] = "中奖啦";
for (int i = 0; i < SIZE; i++) {
listget(i)setText(info[i]);
}
if(((JButton)egetSource())getText()equals("中奖啦")){
JOptionPaneshowMessageDialog(null, "恭喜您,中奖啦!");
}else{JOptionPaneshowMessageDialog(null, "很遗憾,您没有中奖!");}
flag = false;
}else{
info[index] = "谢谢参与";
for (int i = 0; i < SIZE; i++) {
listget(i)setText("");
}
flag = true;
}
}
});
return button;
}
}
帮你写了,你运行看看。
我写给你,如果是本专业,以后要多多学习。JDK 16
import javaawtGridLayout;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javautilIterator;
import javautilRandom;
import javautilSet;
import javautilTreeSet;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJTextField;
public class LuckyDrawn {
private JFrame f = new JFrame("Lucky Drawn");
private JButton btn = new JButton("Drawn");
private JTextField result = new JTextField("Result");
private static final int MIN = 1;
private static final int MAX = 30;
private static final int RESULT_COUNT = 7;
public LuckyDrawn(){
fadd(btn);
fadd(result);
resultsetEditable(false);
btnaddMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Random rand = new Random();
Set<Integer> set = new TreeSet<Integer>();
while(setsize() < RESULT_COUNT){
setadd(new Integer(randnextInt(MAX)+ 1));
}
StringBuffer sb = new StringBuffer("Lucky numbers are: ");
for (Iterator iter = setiterator(); iterhasNext();) {
sbappend(((Integer) iternext())intValue())append(", ");
}
resultsetText(sbsubstring(0, sblength() - 2)toString());
fpack();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
fsetLayout(new GridLayout(2, 1));
fsetVisible(true);
fsetLocation(500, 200);
fpack();
fvalidate();
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LuckyDrawn();
}
}
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);
}
}
程序运行结果截图
中奖的人是随机的!
import javautilScanner;
public class Choujiang {
public static void main(String[] args) {
Scanner sc = new Scanner(Systemin);//定义Scanner类1234
Systemoutprintln("请输入四位卡号:");//输入提示语
int cardNo = scnextInt();//接受输入数据
int gewei = cardNo%10;//分解个位
int shiwei = cardNo/10%10;//分解十位
int baiwei = cardNo/100%10;//分解百位
int qianwei = cardNo/1000;//分解千位
int sum = gewei+shiwei+baiwei+qianwei;
Systemoutprintln("会员卡号四位之和是:"+sum);
if(sum>20){//判断各位数和是否大于20
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;}
以上就是关于用java怎么实现控制中奖率全部的内容,包括:用java怎么实现控制中奖率、用JAVA程序实现,随即抽奖的九个按钮,其中有一个有奖,每次随机出现一个有奖的按钮、用java编写一个彩票抽奖程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)