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");
}
}
}
public class Test {
public static void main(String[] args) {
double d = Mathrandom();//生成一个0~1的随机数
if(d<=05){
Systemoutprintln("A");//50%概率
}else if(d<=08){
Systemoutprintln("B");//30%概率
}else{
Systemoutprintln("C");//20%概率
}
}
}
public int getNum(){
Random random=new Random();
int i=randomnextInt(100);
Systemoutprintln(i);
if(i<25){
return 7;
}else if(i<60){
return 8;
}else{
return 9;
}
}
import javautil;
class Test {
public static void main(String[] args) {
Random rand = new Random();
int[] a = new int[200];
int count=0;
for(int i=0;i<alength;i++){
a[i] = Mathabs(randnextInt(900))+100;
Systemoutprint(a[i]+"\t");
if(a[i]%2==0){
count++;
}
if(i%10==9){
Systemoutprintln();
}
}
Systemoutprintln("偶数个数为:"+count);
Systemoutprintln("偶数概率为:"+(count/2000));
}
}
原理:
根据圆面积的公式: s=πR2 ,当R=1时,S=π。
由于圆的方程是:x^2+y^2=1(x^2为X的平方的意思),因此1/4圆面积为x轴、y轴和上述方程所包围的部分。
如果在11的矩形中均匀地落入随机点,则落入1/4园中的点的概率就是1/4圆的面积。其4倍,就是圆面积。
由于半径为1,该面积的值为π的值。
程序如下:
import javautilRandom;
public class pai {
/
@param args
/
public static void main(String[] args) {
// TODO Auto-generated method stub
int N = 100000; /定义随机点数/
int n = 0,i,resulttimes;
double r;
double x,y; /坐标/
Random s = new Random();
for(resulttimes=0;resulttimes<10;resulttimes++){ /输出十次结果/
for (i=1;i<=N;i++)
{
x=snextDouble(); /在0~1之间产生一个随机x坐标/
y=snextDouble(); /在0~1之间产生一个随机y坐标/
if(xx+yy<=10) n++; /统计落入单位圆中的点数/
}
r=(double)n/N;
Systemoutprintln("The result of pai is "+r4); /计算出π的值/
n=0;
}
}
}
以上就是关于java抽奖程序,可设置不同奖品获得概率全部的内容,包括:java抽奖程序,可设置不同奖品获得概率、java中如何让A的输出的概率为50%B输出的概率为30%C输出的概率为20%、java写一个方法返回7、8、9三个数,且要求返回的概率依次为25%、35%、40%等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)