如何在for循环中放置2个for循环

时间:2017-11-02 19:17:18

标签: java loops

因此,如果一个女人生了一个男孩,她就只能生多个孩子 - 如果她生了一个女孩,她就再也没有孩子了。我必须创建一个表格,显示运行10个模拟的结果,每个模拟包括根据此政策生育的10,000名母亲的男孩与女孩的比例。我们假设每个出生都会产生一个孩子(没有双胞胎,三胞胎等),每个孩子都会生活,并且生男孩的几率是50%。

到目前为止,我有:

    for (numSimulation = 0; numSimulation < 10; numSimulation++){;
        for (int mothers = 1; mothers <= 10000; mothers++){;
            int randomNum = randomGenerator.nextInt();

            boolean isMale = randomNum % 2 == 0;

            if (isMale){
                numMales++;
            }
            else{
                numFemales++;
            }}
        femaleToMaleRatio =(double) numFemales / numMales;

    }

    System.out.printf("Run#  M : F%n");
    System.out.printf("%4d  1 : %.5f%n",numSimulation-9, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-8, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-7, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-6, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-5, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-4, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-3, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-2, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation-1, femaleToMaleRatio);
    System.out.printf("%4d  1 : %.5f%n",numSimulation, femaleToMaleRatio);

}}`

我必须在第三个循环中放置什么以及什么才能让它继续运行直到它生下一个女孩?而且我如何展示每个numSimuation独有的femaleToMaleRatio?因为现在它在我的System.out.printf

上显示了所有10次运行的相同数字

5 个答案:

答案 0 :(得分:1)

我会做一段时间(!isFemale){//做随机数发生器,如果男性然后男性++如果女性然后增加女性变量并将女性布尔值设置为真。下次它试图进入while循环时它会停止因为女性是真的。}这个while循环应该在其他两个for循环之后。希望这会有所帮助。

答案 1 :(得分:1)

希望它可以帮助你们所有人

public static void main(String[] args) throws IOException {
        int numMales = 0;
        int numFemales = 0;
        int numSimulation = 0;
        double femaleToMaleRatio = 0;

        System.out.printf("Run#  M : F%n");

        for (numSimulation = 0; numSimulation < 10; numSimulation++) {
            for (int mothers = 1; mothers <= 10000; mothers++) {
                int randomNum = randInt(0,1);

                boolean isMale = randomNum % 2 == 0;

                boolean isFemale = false;
                while(!isFemale){
                    isFemale = randInt(0,1) == 0;
                }

                if (isMale) {
                    numMales++;
                } else {
                    numFemales++;
                }
            }
            femaleToMaleRatio = (double) numFemales / numMales;

            System.out.printf("%4d  1 : %.15f%n", numSimulation, femaleToMaleRatio);
        }

    }

    public static int randInt(int min, int max) {
        Random rand = new Random();

        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

答案 2 :(得分:0)

试试这个希望这有帮助

for (numSimulation = 0; numSimulation < 10; numSimulation++){
       boolean isfemale=false;
        for (int mothers = 1; mothers <= 10000; mothers++){
            int randomNum = randomGenerator.nextInt();
         do{
            boolean isMale = randomNum % 2 == 0;

            if (isMale){
                numMales++;
            }
            else{
                isfemale=true;
                numFemales++;
            }
         }while(!isfemale);
        }
        femaleToMaleRatio =(double) numFemales / numMales;

    }

答案 3 :(得分:0)

好的,所以前两个循环是正确的,除了其他人指出的最后一个分号。

现在为了生育。一位母亲将分娩。如果她生了一个男孩,她会继续分娩。如果她生了一个女孩,她就会停下来。正确?这可以在while循环中反映出来:

while(randomGenerator.nextInt() % 2 == 0) { // while we're giving birth to boys
  numMales++; // increment the boys counter
}
numFemales++; // we've stopped giving birth to boys -> we've given birth to a girl and should stop

现在显示每次模拟的雌雄比例。在每次模拟之后,即第一次循环结束时,Simpy输出雌雄比例。

完整的代码可能看起来像这样

for (int numSimulation = 0; numSimulation < 10; numSimulation++) {

  // reset the boys and girls counter before each simulation
  int numMales = 0;
  int numFemales = 0;
  double femaleToMaleRatio = 1;

    for (int mothers = 1; mothers <= 10000; mothers++) { // here the simulation starts

      while(randomGenerator.nextInt() % 2 == 0) { // while we're giving birth to boys
          numMales++; // increment the boys counter
      }
      // we've stopped giving birth to boys -> we've given birth to a girl and should stop
      numFemales++;
    }

  // we only need to calculate the female to male ratio after the simulation is done
  femaleToMaleRatio = (double) numFemales / numMales;

  // then we print the result of our simulation once its done
  System.out.printf("%4d  1 : %.5f%n", numSimulation, femaleToMaleRatio);
}

希望这有帮助。

答案 4 :(得分:0)

建立Ashwin Shenoy所说的话: 为了做到这一点,你必须在有一个男孩,而不是一个女孩后,基本上做一个递归调用(或一个while语句)。

这看起来像是:

public static double getFemaleToMaleRatio() {
    // total number of runs
    int numberOfRuns = 0;
    // total number of females
    int numFemales = 0;
    // total number of males
    int numMales = 0;

    // run n number of times
    while(numberOfRuns < 10) {
        int mothers = 0;
        while(mothers < 10000) {
            // keep running here until "makeBabyGirl()" returns true
            while(!makeBabyGirl()) {
                numMales++;
            }

            // once "makeBabyGirl()" returns true, it breaks the while loop and increments "numFemales"
            numFemales++;
            mothers++;
        }
        numberOfRuns++;
        System.out.printf("Ratio: %s\n", (double) numFemales/ numMales);
    }
    System.out.printf("Final Ratio: %s\n", (double) numFemales/ numMales);
    return (double) numFemales/ numMales;
}

public static boolean makeBabyGirl() {
    // 50% change of having a boy or girl
    boolean isFemale = Math.random() < 0.5;
    return isFemale;
}

你可以留下你的2 for循环,只需添加新的while循环来检查宝宝是否也是女性。