x 패밀리의 입력을 넣으려는 코드를 시도 할 때까지 프로그램에서 각 패밀리에게 아기가 있어야합니다 (50/50의 성 중 하나를 가질 기회가 있음). 각 성별 중 하나가 성취되었습니다. 그런 다음 2, 3, 4, 5 명 또는 그 이상의 아기가있는 가족의 수와 목표를 달성하는 데 가장 많이 걸리는 아기의 수를 평균을 출력해야합니다. 나는 while 진술 (if 문 클러스터의 어딘가) 다음에 엉망이라고 믿는다. 어떤 도움이라도 대단히 감사합니다. 감사!"x"패밀리에서 아기 만들기
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 + ".");
}
}
이 숙제가 있습니까? – Zenadix
@ Zenadix 예. 내가 얻을 수있는 도움을 모호하게 찾고 있습니다. 나는 도움을 받기 위해 실험실에 갈 시간을 찾지 못했습니다. –
아기 만들기 = 숙제, 나는 잘못된 학위를 가지고있다 :-) – Leo