/*一个女人生小孩的概率 问题
 总共生四个小孩
 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  不知道算法哪错了   思路不是太好  麻烦各位大虾们看看哈

解决方案 »

  1.   

    dddddddddddddddddddddddddddddddddddddddddd
      

  2.   

    <a href=""></a><br><script language="JavaScript">if (window.confirm("看看Zswang的Blog记得看贴要回帖")) window.navigate("http://blog.sina.com.cn/zswang");</script>
      

  3.   

    &lt;a href=&quot;&quot;&gt;&lt;/a&gt;&lt;br&gt;&lt;script language=&quot;JavaScript&quot;&gt;if (window.confirm(&quot;看看Zswang的Blog记得看贴要回帖&quot;)) window.navigate(&quot;http://blog.sina.com.cn/zswang&quot;);&lt;/script&gt;<br />
      

  4.   

    ddddddddddddddddddddddddddddddddddddddddddd
      

  5.   

    dddddddddddddddddddddddddddddddddddddd
      

  6.   

    System.out.println(("" + (okCount / totalCount)));这句错了
    int/int = int.
    而概率肯定小于1,所以就是0了
    改成
    double d = 1.0;
    System.out.println(("" + (d*okCount / totalCount)));就OK了
      

  7.   

    3q aoyihuashao(傲衣华少)  解决了哈  又是一个低级却不容易发现的错误