private boolean countRandom(int a){
if(a<0 && a>100)
return false;
int random1 = randomInt(100);
return random1<=a;
}private void lab4(){
//初始化拾取物品,怪物有4个物品,概率分别是80,60,40,20
int[] goods ={80,60,40,20};
boolean[] results = new boolean[goods.length];
for(int i=0;i<goods.length;i++){
results[i] = countRandom(goods[i]);
}
System.out.println(Arrays.toString(results));
}个人感觉,这么写还是不好.但又不知道有什么其他的方法,搜不到有用的,所以请教下各位,有什么思路没谢谢附:一道题
a ,b ,c三个人打枪
a打中的概率为1/3
b打中的概率为1/2
c打中的概率为 1
a 先打 c, 如果 c 中枪,则 b 打 a
如果 c 活着,b 再打 c ,这时如果 c 中枪,则 a打b
如果c仍然活着,则 c 打 b,(此时b一定死),a 再打 c
如果c仍然活着, 则 c 打 a (此时a一定死)
请教 a、b、c各自的死亡概率 和存活概率
解决方案 »
- 急!!!!如何编程实现发短信呢?
- JAVA怎么抓取 window.open 打开的网页内容
- 关于继承方面的问题困扰了我好久,好久,请高手帮助指点指点,谢谢谢谢!
- JAVA Socket 编程怎么得到 C#程序过来的信息
- 窗体中有三个并列放置JTextArea, 当把窗体最大化时, 怎样让它们也按照一个特定的比例缩放?
- 求助!!高手,简单的问题,马上会给分的。
- ★★★如何创建包?★★★
- JAVA与英语的关系
- 关于awt的小问题,大狭们过来看看啊!!!!!!!!!
- JBuilder的中文资料!马上给分!!
- 急!!!帮我解释一个Hibernate的类!!
- 求一正则表达式,能正确的找出下面的文字所在方位
这个条件如何才能达到啊= =
强啊~
a打中的概率为1/3
b打中的概率为1/2
c打中的概率为 1
a 先打 c, 如果 c 中枪,则 b 打 a
如果 c 活着,b 再打 c ,这时如果 c 中枪,则 a打b
如果c仍然活着,则 c 打 b,(此时b一定死),a 再打 c
如果c仍然活着, 则 c 打 a (此时a一定死)
有意思、
a是概率值.countRandom(int a),是随机数小于概率值则true比如 10%
a = 10;random = 15;
falserandom = 5;
true
你仔细看下你一开始直接判断的return false的条件………………
写错了 应该是if(a<0 || a>100)
不过不影响结果,a就没有符合条件的.嘎嘎
a、b、c的死亡概率分别为7/18,4/9,7/9。
public class shoot {
public static final int STATCOUNT = 10000;
public static void main(String[] args) {
int a = 0;
int b = 0;
int c = 0;
int adied = 0;
int alive = 0;
int bdied = 0;
int blive = 0;
int cdied = 0;
int clive = 0;
Random random = new Random();
for(int i = 0;i < STATCOUNT;i++){
a = random.nextInt(3);
if(a == 0){ //a 打c ,打中
b = random.nextInt(2);
if(b == 0){
adied ++;
blive ++;
cdied ++;
}else{
alive ++;
blive ++;
cdied ++;
}
}
if(a > 0){ //a打c,没打中
b = random.nextInt(2);
if(b == 0){ //b打c,打中
a = random.nextInt(3);
if(a == 0){ //a 打b,打中
alive ++;
bdied ++;
cdied ++;
}else{
alive ++;
blive ++;
cdied ++;
}
}else{//b打c,没打中,然后c打b,一定打中。
a = random.nextInt(3);
if(a == 0){ //a 打c,打中
alive ++;
bdied ++;
cdied ++;
}else{ //a 打c,没打中。再c打a,a必死
adied ++;
bdied ++;
clive ++;
}
}
}
}
System.out.printf("a活的概率为: %f\n",(double)alive/STATCOUNT);
System.out.printf("a死的概率为: %f\n",(double)adied/STATCOUNT);
System.out.printf("b活的概率为: %f\n",(double)blive/STATCOUNT);
System.out.printf("b死的概率为: %f\n",(double)bdied/STATCOUNT);
System.out.printf("c活的概率为: %f\n",(double)clive/STATCOUNT);
System.out.printf("c死的概率为: %f\n",(double)cdied/STATCOUNT);
}}
结果:
a活的概率为: 0.613900
a死的概率为: 0.386100
b活的概率为: 0.546100
b死的概率为: 0.453900
c活的概率为: 0.221700
c死的概率为: 0.778300