java抽奖程序,可设置不同奖品获得概率
需要一个java的抽奖算法,并且可以设置中奖几率,几率设定如下物品1,10物品2,20物品3,30那么物品1的中奖几率就是10/60=1/6物品2的中奖几率就是20/60...
需要一个java的抽奖算法,并且可以设置中奖几率,几率设定如下
物品1,10
物品2,20
物品3,30
那么物品1的中奖几率就是10/60=1/6
物品2的中奖几率就是20/60=1/3
每个物品的中奖几率是当前物品几率值除以总几率值. 展开
物品1,10
物品2,20
物品3,30
那么物品1的中奖几率就是10/60=1/6
物品2的中奖几率就是20/60=1/3
每个物品的中奖几率是当前物品几率值除以总几率值. 展开
1个回答
展开全部
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
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) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LuckySelect frame = new LuckySelect();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LuckySelect() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 251);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JLabel lblA = new JLabel("A");
lblA.setBounds(10, 128, 54, 15);
contentPane.add(lblA);
JLabel lblB = new JLabel("B");
lblB.setBounds(124, 128, 54, 15);
contentPane.add(lblB);
JLabel lblC = new JLabel("C");
lblC.setBounds(254, 128, 54, 15);
contentPane.add(lblC);
textFieldA = new JTextField();
textFieldA.setBounds(30, 125, 66, 21);
contentPane.add(textFieldA);
textFieldA.setColumns(10);
textFieldB = new JTextField();
textFieldB.setColumns(10);
textFieldB.setBounds(149, 125, 66, 21);
contentPane.add(textFieldB);
textFieldC = new JTextField();
textFieldC.setColumns(10);
textFieldC.setBounds(264, 125, 66, 21);
contentPane.add(textFieldC);
textField = new TextField();
textField.setBounds(98, 167, 157, 21);
contentPane.add(textField);
textField.setColumns(10);
textFieldResult = new JTextField();
textFieldResult.setBounds(280, 167, 66, 21);
contentPane.add(textFieldResult);
textFieldResult.setColumns(10);
textFieldA.setText("10");
textFieldB.setText("10");
textFieldC.setText("10");
JButton button = new JButton("\u62BD\u5956");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
select();
}
});
button.setBounds(0, 166, 93, 23);
contentPane.add(button);
textArea = new JTextArea();
textArea.setBounds(30, 31, 306, 83);
contentPane.add(textArea);
}
protected void select() {
// TODO Auto-generated method stub
int aNum = Integer.decode(textFieldA.getText());
int bNum = Integer.decode(textFieldB.getText());
int cNum = Integer.decode(textFieldB.getText());
Random r = new Random();
int random = r.nextInt(aNum + bNum + cNum);
if(random <= aNum){
textFieldA.setText(Integer.toString(Integer.decode(textFieldA.getText()) - 1));
textArea.append(Integer.toString(random) + "抽中了A\n");
}else if(random <= aNum + bNum){
textFieldB.setText(Integer.toString(Integer.decode(textFieldB.getText()) - 1));
textArea.append(Integer.toString(random) + "抽中了B\n");
}else if(random <= aNum + bNum + cNum){
textFieldC.setText(Integer.toString(Integer.decode(textFieldC.getText()) - 1));
textArea.append(Integer.toString(random) + "抽中了C\n");
}
}
}
追问
能加点注释吗= =||
追答
思路就是每按下一次按钮,就获得A,B,C当前的数量,然后产生随机数,根据当前数目设定 if 语句判断的阈值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询