码迷,mamicode.com
首页 > 编程语言 > 详细

一个家庭两个孩子,已知一个是女孩,另一个是女孩的概率是?Java

时间:2019-07-14 00:07:07      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:col   string   void   class   tin   int   hello   random   new   

下学期学java了,先拿C++程序改编一下练练手

package helloworld;
import java.util.Random;
public class hello {
        public static void main(String[] args) {
            for(int i = 0; i < n; ++i){
                Random x1 = new Random();
                Random x2 = new Random();
                int r1 = x1.nextInt(2);
                int r2 = x2.nextInt(2);
                if(r1 == girl && r2 == girl) g2++;
                if(r1 == girl || r2 == girl) g1++;
            }
            System.out.println(g2 * 1.0 / g1);
        } 
        static int girl = 0, boy = 1;
        static int g1 = 0;
        static int g2 = 0;
        static int n = 10000;
}

 

一个家庭两个孩子,已知一个是女孩,另一个是女孩的概率是?Java

标签:col   string   void   class   tin   int   hello   random   new   

原文地址:https://www.cnblogs.com/MasterYan576356467/p/11182649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!