/*一个女人生小孩的概率 问题
总共生四个小孩
a 都是 男的 或都是女的
b 两个男的 和两个女的
c 三个男的和一个女的 或三个女的和一个男的
求a b c 的概率
只用到 if 和 for语句
和Math.random这个函数!~ */public class Probability { public int totalCount = 100000;// 总循环次数 // 都是 男的 或都是女的
public void viewA() {
int okCount = 0;
for (int i = 0; i < totalCount; i++) {
int temp = 0;
for (int j = 0; j < 4; j++)
temp += (int) (Math.random() * 10 + 1);
if (temp == 40)
okCount++;
if (temp == 4)
okCount++;
}
System.out.println(("" + (okCount / totalCount)));
} // 两个男的 和两个女的
public void viewB() {
int okCount = 0;
for (int i = 0; i < totalCount; i++) {
int man = 0, woman = 0;
int[] temp = { (int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1) };
for (int j = 0; j < 4; j++) {
if (temp[j] == 10)
man++;
else if (temp[j] == 1)
woman++;
else
continue;
}
if (man == 2 && woman == 2)
okCount++;
}
System.out.println(("" + (okCount / totalCount)));
} // 三个男的和一个女的 或三个女的和一个男的
public void viewC() {
int okCount = 0;
for (int i = 0; i < totalCount; i++) {
int man = 0, woman = 0;
int[] temp = { (int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1) };
for (int j = 0; j < 4; j++) {
if (temp[j] == 10)
man++;
else if (temp[j] == 1)
woman++;
else
continue;
}
if ((man == 3 && woman == 1) || (man == 1 && woman == 3))
okCount++;
}
System.out.println(("" + (okCount / totalCount)));
} public static void main(String[] args) {
Probability p = new Probability();
p.viewA();
p.viewB();
p.viewC();
}
}
算出来都是0 不知道算法哪错了 思路不是太好 麻烦各位大虾们看看哈
总共生四个小孩
a 都是 男的 或都是女的
b 两个男的 和两个女的
c 三个男的和一个女的 或三个女的和一个男的
求a b c 的概率
只用到 if 和 for语句
和Math.random这个函数!~ */public class Probability { public int totalCount = 100000;// 总循环次数 // 都是 男的 或都是女的
public void viewA() {
int okCount = 0;
for (int i = 0; i < totalCount; i++) {
int temp = 0;
for (int j = 0; j < 4; j++)
temp += (int) (Math.random() * 10 + 1);
if (temp == 40)
okCount++;
if (temp == 4)
okCount++;
}
System.out.println(("" + (okCount / totalCount)));
} // 两个男的 和两个女的
public void viewB() {
int okCount = 0;
for (int i = 0; i < totalCount; i++) {
int man = 0, woman = 0;
int[] temp = { (int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1) };
for (int j = 0; j < 4; j++) {
if (temp[j] == 10)
man++;
else if (temp[j] == 1)
woman++;
else
continue;
}
if (man == 2 && woman == 2)
okCount++;
}
System.out.println(("" + (okCount / totalCount)));
} // 三个男的和一个女的 或三个女的和一个男的
public void viewC() {
int okCount = 0;
for (int i = 0; i < totalCount; i++) {
int man = 0, woman = 0;
int[] temp = { (int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1) };
for (int j = 0; j < 4; j++) {
if (temp[j] == 10)
man++;
else if (temp[j] == 1)
woman++;
else
continue;
}
if ((man == 3 && woman == 1) || (man == 1 && woman == 3))
okCount++;
}
System.out.println(("" + (okCount / totalCount)));
} public static void main(String[] args) {
Probability p = new Probability();
p.viewA();
p.viewB();
p.viewC();
}
}
算出来都是0 不知道算法哪错了 思路不是太好 麻烦各位大虾们看看哈
解决方案 »
- 初识UDP的协议 解包问题 目前在byte[]的操作上有问题
- 大家帮帮忙,看看这几道面试题应该选什么?
- java操作Excle2007该用什么框架及jxl的异常问题?
- JAVA基础应用: 处理Java中的日期问题
- Socket FTP 流的同步问题,本地流用FTPClient向服务端写文件后,服务端的文件不完整
- java 环境变量在WINDOWS-XP中怎么设置
- java记事本查找时,如何同时标记全部?
- 虚心请教:我想创建变长的Hashtable数组,Hashtable mp[]; mp[0] = new Hashtable();为什么错了呢?
- 如何实现%%文件读取
- csdn 这种带颜色的table ,在网页中是如何写的???
- 输入流
- 抽象类中的方法如何处理
int/int = int.
而概率肯定小于1,所以就是0了
改成
double d = 1.0;
System.out.println(("" + (d*okCount / totalCount)));就OK了