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);
}
java-三门试验
最新推荐文章于 2024-10-05 12:07:50 发布