java-三门试验

public static void main(String[] args) {
        //存放没换获得豪车的概率
        int noExchange=0;
        //存放交换了获得豪车的概率
        int Exchange=0;
        //未交换
        String noString;
        //已交换
        String edString;
        for (int i=0; i<100000; i++) {
            ArrayList<String> strings = new ArrayList<>();
            //集合里面一共有的情况
            //1.有,没有,没有
            //2.没有,没有,有
            //3.没有,有,没有
            Random random = new Random();
            //随机取0或者1
            int first = random.nextInt(2);
            //0表示没有,1表示有,添加到集合第一个数
            if(first == 1) {
                strings.add("有");
                //添加第二,三个数,如果第一个数表示没有,那么后面两个都是有
                strings.add("没有");
                strings.add("没有");
            }else{
                strings.add("没有");
                //随机取0或者1
                int second = random.nextInt(2);
                //0表示没有,1表示有,添加到集合第一个数
                if(second == 0){
                    strings.add("没有");
                    strings.add("有");
                }else{
                    strings.add("有");
                    strings.add("没有");
                }
            }
            //随机获得0,1,2获取集合一个数值
            int getNum = random.nextInt(3);
            //未交换
            noString = strings.get(getNum);
            //已交换
            edString = "";
            if(getNum == 0){
                //获取到的是第一个数
                if(strings.get(1).equals("没有")){
                    edString=strings.get(2);
                }else{
                    edString=strings.get(1);
                }
            }else if(getNum == 1){
                //获取到的是第二个数
                if(strings.get(0).equals("没有")){
                    edString=strings.get(2);
                }else{
                    edString=strings.get(0);
                }
            }else if(getNum == 2){
                //获取到的是第三个数
                if(strings.get(0).equals("没有")){
                    edString=strings.get(1);
                }else{
                    edString=strings.get(0);
                }
            }
            if(noString.equals("有")){
                noExchange++;
            }
            if(edString.equals("有")){
                Exchange++;
            }
        }
        System.out.println("没换的数量"+noExchange);
        System.out.println("换了的数量"+Exchange);
    }

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值