我正在尝试一个代码,其中您输入了x个家庭,然后该程序应让每个家庭都有孩子(有50/50的机会生两性之一),直到达到每一种性别。然后,应输出平均值,有2、3、4和5个或更多婴儿的家庭数量,以及达到目标所需的最常见婴儿数量。我相信我在while语句之后(在if语句簇中的某个位置)搞砸了。任何帮助是极大的赞赏。谢谢!

public class BoysAndGirls {

    public static void main(String[] args) {
        int families = Integer.parseInt(args[0]);
        int boy = 0;
        int girl = 0;
        int fam2 = 0;
        int fam3 = 0;
        int fam4 = 0;
        int fam5 = 0;
        int total = 0;

        for (int i = 0; i < families; i++) {
            while ((boy <= 1) || (girl <= 1)) {
                if (Math.random() < 0.5) {
                    boy = boy + 1;
                } else {
                    girl = girl + 1;
                }
                total++;
            }

            if (total == 2) {
                fam2++;
            }
            if (total == 3) {
                fam3++;
            }
            if (total == 4) {
                fam4++;
            }
            if (total >= 5) {
                fam5++;
            }
        }

        double average = total / families;
        System.out.println("Average: " + average + " babies were had to get at least one of each sex.");
        System.out.println("Number of families with 2 children: " + fam2);
        System.out.println("Number of families with 3 children: " + fam3);
        System.out.println("Number of families with 4 children: " + fam4);
        System.out.println("Number of families with 5 or more children: " + fam5);
        //System.out.println("The most common number of children was " + common + ".");
    }
}

最佳答案

好的,您应该进行一些小更改。

您应该有另一个变量,假设currentTotal跟踪单个家庭当前的孩子总数,并使用另一个变量total跟踪所有家庭的孩子总数。

同样在循环开始时,您需要将计数器boygirlcurrentTotal重置为零,因为它们对单个系列的信息进行计数。

最后,当计算孩子的平均数时,由于整数除法不是很精确,因此需要将int total double改为fam

代替:

double average = total / families;


放:

double average = (double) total / families;


将所有这些修改应用于您的代码后,我们得到:

    public static void main(String[] args) {

    int families = Integer.parseInt(args[0]);
    int boy;
    int girl;
    int fam2 = 0;
    int fam3 = 0;
    int fam4 = 0;
    int fam5 = 0;
    int total = 0;
    int currentTotal;
    for (int i=0; i < families; i++){
        currentTotal = 0;
        boy = 0;
        girl = 0;
        while ( (boy < 1) || (girl < 1) ){
            if (Math.random() < 0.5) {
                boy = boy + 1;
            }
            else {
                girl = girl + 1;
            }
            currentTotal++;
            total++;
        }
        if (currentTotal == 2){
            fam2++;
        }
        if (currentTotal == 3){
            fam3++;
        }
        if (currentTotal == 4){
            fam4++;
        }
        if (currentTotal >= 5){
            fam5++;
        }
    }
    double average = (double) total / families;
    System.out.println("Average: " + average + " babies were had to get at least one of each sex.");
    System.out.println("Number of families with 2 children: " + fam2);
    System.out.println("Number of families with 3 children: " + fam3);
    System.out.println("Number of families with 4 children: " + fam4);
    System.out.println("Number of families with 5 or more children: " + fam5);
    //System.out.println("The most common number of children was " + common + ".");
}


输入为10时的样本输出

Average: 2.7 babies were had to get at least one of each sex.
Number of families with 2 children: 7
Number of families with 3 children: 2
Number of families with 4 children: 0
Number of families with 5 or more children: 1


编辑:

要找到最常见的婴儿数量,您只需在所有fam2变量中找到最大值。您可以按照自己的方式进行操作,以下是我对您的建议。将顶部的代码更改为大小为4的数组:

int[] fams = new int[4];


而不是每个fam变量(fam3fam4fam5if)。

然后在循环中,无论currentTotal是什么,您都可以使用以下方法覆盖所有情况(替换所有fams语句):

if(currentTotal >= 5) {
    fams[3]++;  // 5 or more children. index 3 is the 4th element
} else {
    fams[currentTotal - 2]++; // currentTotal - 2 because we are offsetting by 2
}


然后,您可以通过在数组中找到最大值来找到最常见的元素。

int common = 0;
for(int i = 0; i < fams.length; i++) {
    if(common < fams[i]) {
        common = fams[i];
    }
}
// at this point, common would hold the most common number of kids

10-08 02:47