import javaawtEventQueue;
import javaawtTextArea;
import javaawtTextField;
import javaawteventActionEvent;
import javaawteventActionListener;
import javautilRandom;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJTextArea;
import javaxswingJTextField;
import javaxswingborderEmptyBorder;
public class LuckySelect extends JFrame {
private JPanel contentPane;
private JTextField textFieldA;
private JTextField textFieldB;
private JTextField textFieldC;
private TextField textField;
private JTextField textFieldResult;
private JTextArea textArea;
/
Launch the application
/
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
public void run() {
try {
LuckySelect frame = new LuckySelect();
framesetVisible(true);
} catch (Exception e) {
eprintStackTrace();
}
}
});
}
/
Create the frame
/
public LuckySelect() {
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setBounds(100, 100, 450, 251);
contentPane = new JPanel();
contentPanesetBorder(new EmptyBorder(5, 5, 5, 5));
contentPanesetLayout(null);
setContentPane(contentPane);
JLabel lblA = new JLabel("A");
lblAsetBounds(10, 128, 54, 15);
contentPaneadd(lblA);
JLabel lblB = new JLabel("B");
lblBsetBounds(124, 128, 54, 15);
contentPaneadd(lblB);
JLabel lblC = new JLabel("C");
lblCsetBounds(254, 128, 54, 15);
contentPaneadd(lblC);
textFieldA = new JTextField();
textFieldAsetBounds(30, 125, 66, 21);
contentPaneadd(textFieldA);
textFieldAsetColumns(10);
textFieldB = new JTextField();
textFieldBsetColumns(10);
textFieldBsetBounds(149, 125, 66, 21);
contentPaneadd(textFieldB);
textFieldC = new JTextField();
textFieldCsetColumns(10);
textFieldCsetBounds(264, 125, 66, 21);
contentPaneadd(textFieldC);
textField = new TextField();
textFieldsetBounds(98, 167, 157, 21);
contentPaneadd(textField);
textFieldsetColumns(10);
textFieldResult = new JTextField();
textFieldResultsetBounds(280, 167, 66, 21);
contentPaneadd(textFieldResult);
textFieldResultsetColumns(10);
textFieldAsetText("10");
textFieldBsetText("10");
textFieldCsetText("10");
JButton button = new JButton("\u62BD\u5956");
buttonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
select();
}
});
buttonsetBounds(0, 166, 93, 23);
contentPaneadd(button);
textArea = new JTextArea();
textAreasetBounds(30, 31, 306, 83);
contentPaneadd(textArea);
}
protected void select() {
// TODO Auto-generated method stub
int aNum = Integerdecode(textFieldAgetText());
int bNum = Integerdecode(textFieldBgetText());
int cNum = Integerdecode(textFieldBgetText());
Random r = new Random();
int random = rnextInt(aNum + bNum + cNum);
if(random <= aNum){
textFieldAsetText(IntegertoString(Integerdecode(textFieldAgetText()) - 1));
textAreaappend(IntegertoString(random) + "抽中了A\n");
}else if(random <= aNum + bNum){
textFieldBsetText(IntegertoString(Integerdecode(textFieldBgetText()) - 1));
textAreaappend(IntegertoString(random) + "抽中了B\n");
}else if(random <= aNum + bNum + cNum){
textFieldCsetText(IntegertoString(Integerdecode(textFieldCgetText()) - 1));
textAreaappend(IntegertoString(random) + "抽中了C\n");
}
}
}
用一个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;}
import javautilScanner;
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(Systemin);
do {
Systemoutprintln("欢迎进入奖客富翁系统");
Systemoutprintln("\t1注册");
Systemoutprintln("\t2登录");
Systemoutprintln("\t3抽奖");
Systemoutprintln("");
Systemoutprint("请选择菜单:");
int choice =
inputnextInt();
switch
(choice) {
case 1:
Systemoutprintln("[奖客富翁系统
> 注册]");
Systemoutprintln("请填写个人注册信息:");
Systemoutprint("用户名:");
userName =
inputnext();
Systemoutprint("密码:");
password =
inputnext();
//
获取4位随机数作为卡号
cardNumber =
(int)(Mathrandom()(max-min))+min;
Systemoutprintln("\n注册成功,请记好您的会员卡号");
Systemoutprintln("用户名\t密码\t会员卡号");
Systemoutprintln(userName
+ "\t" + password + "\t" + cardNumber);
isRegister =
true; // 注册成功,标志位设置为true
break;
case 2:
Systemoutprintln("[奖客富翁系统
> 登录]");
if
(isRegister) { // 判断是否注册
//
3次输入机会
for (int i
= 1; i <= 3; i++) {
Systemoutprint("请输入用户名:");
String
inputName = inputnext();
Systemoutprint("请输入密码:");
String
inputPassword = inputnext();
if
(userNameequals(inputName) && passwordequals(inputPassword)) {
Systemoutprintln("\n欢迎您:"
+ userName);
isLogin =
true; // 登录成功,标志位设置为true
break;
} else if
(i < 3) {
Systemoutprintln("用户名或密码错误,还有"
+ (3 - i) + "次机会!");
} else
{
Systemoutprintln("您3次均输入错误!");
}
}
} else
{
Systemoutprintln("请先注册,再登录!");
}
break;
case 3:
Systemoutprintln("[奖客富翁系统
> 抽奖]");
if
(!isLogin) { // 判断是否登录
Systemoutprintln("请先登录,再抽奖!");
} else
{
//生成5个4位随机数字,并保存在数组中
int[]
luckynums = new int[5];
for(int i
= 0; i < luckynumslength; i++){
luckynums[i] =
(int)(Mathrandom()(max-min))+min;
}
Systemoutprint("请输入您的卡号:");
int
yourcard = inputnextInt();
int
i;
Systemoutprint("\n本日的幸运数字为:");
for (i = 0;
i < luckynumslength; i++) {
Systemoutprint(luckynums[i]
+ " ");\
}
for (i = 0;
i < luckynumslength; i++) {
if
(luckynums[i] == yourcard) {
Systemoutprintln("\n恭喜!您是本日的幸运会员!");
break;
}
}
if (i ==
luckynumslength) {
Systemoutprintln("\n抱歉!您不是本日的幸运会员!");
}
}
break;
default:
Systemoutprintln("[您的输入有误!]");
break;
}
Systemoutprint("继续吗?(y/n):");
answer =
inputnext();
Systemoutprintln("");
} while
("y"equals(answer));
if
("n"equals(answer)) {
Systemoutprintln("系统退出,谢谢使用!");
}
}
}
以上就是关于java抽奖程序,可设置不同奖品获得概率全部的内容,包括:java抽奖程序,可设置不同奖品获得概率、用Java 编一个抽奖程序、如何用java技术实现幸运抽奖活动系统等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)