英文:
When my loop repeats, why isn't the random value assigned to a certain variable changing?
问题
System.out.println("两个子女家庭的构成统计:\n");
System.out.println("家庭总数:");
FamilyNumber = Integer.parseInt(in.nextLine());
List<String> list = Arrays.asList(boy, girl);
while (RunCount < FamilyNumber) {
randNum = (int)(Math.random() * 2); // 修改这里,确保随机数在 0 到 1 之间
randNum2 = (int)(Math.random() * 2); // 修改这里,确保随机数在 0 到 1 之间
FirstGender = list.get(randNum);
SecondGender = list.get(randNum2);
GenderValues = FirstGender + SecondGender;
if (GenderValues.equals("BG") || GenderValues.equals("GB")) { // 使用 equals 比较字符串
BGCount++;
}
else if (GenderValues.equals("GG")) { // 使用 equals 比较字符串
GGCount++;
}
else {
BBCount++;
}
RunCount++;
}
GGPercent = ((double)(GGCount) / FamilyNumber) * 100; // 修改这里,确保计算百分比正确
BGPercent = ((double)(BGCount) / FamilyNumber) * 100; // 修改这里,确保计算百分比正确
BBPercent = ((double)(BBCount) / FamilyNumber) * 100; // 修改这里,确保计算百分比正确
System.out.println("有以下类型家庭:\n");
System.out.println("\t两个男孩:" + BBCount + " 占比 " + BBPercent + "%");
System.out.println("\t两个女孩:" + GGCount + " 占比 " + GGPercent + "%");
System.out.println("\t一个男孩和一个女孩:" + BGCount + " 占比 " + BGPercent + "%");
Please note that the main changes include using equals
to compare strings instead of ==
, adjusting the random number generation to ensure it covers the range [0, 1], and modifying the percentage calculation to ensure accurate results.
英文:
System.out.println("Composition Statistics for Families with Two Children: \n");
System.out.println("Total Number of Families: ");
FamilyNumber = Integer.parseInt(in.nextLine());
List<String> list = Arrays.asList(boy, girl);
while (RunCount < FamilyNumber) {
randNum = (int)(Math.random() * 1 + 0);
randNum2 = (int)(Math.random() * 1 + 0);
FirstGender = list.get(randNum);
SecondGender = list.get(randNum2);
GenderValues = FirstGender + SecondGender;
if (GenderValues == "BG" || GenderValues == "GB") {
BGCount++;
}
else if (GenderValues == "GG") {
GGCount ++;
}
else {
BBCount++;
}
RunCount++;
}
GGPercent = ((double)(GGCount/FamilyNumber)*(100));
BGPercent = ((double)(BGCount/FamilyNumber)*(100));
BBPercent = ((double)(BBCount/FamilyNumber)*(100));
System.out.println("Number of Families with: \n");
System.out.println("\tTwo Boys: " + BBCount + " Represents " + BBPercent + "%");
System.out.println("\tTwo Girls: " + GGCount + " Represents " + GGPercent + "%");
System.out.println("\tOne Boy and One Girl: " + BGCount + " Represents " + BGPercent + "%");
This is the segment of code the issue is in. I already initialized all the variables and imported everything necessary. The problem is, whenever I run the program, I get this output:
Composition Statistics for Families with Two Children:
Total Number of Families:
15
Number of Families with:
Two Boys: 15 Represents 100.0%
Two Girls: 0 Represents 0.0%
One Boy and One Girl: 0 Represents 0.0%
The output is always two boys make up all the families. I'm assuming that the issue is with randNum and randNum2 variables, but I'm really not sure. I have no idea what to do so any input on where I'm going wrong is greatly appreciated.
答案1
得分: 2
Math.random返回一个介于0和1之间的数字。
因此,当转换为整数时,它将始终为0。
选择一个缩放因子并将结果乘以该因子(假设为5),
然后结果将是一个介于0和4之间的整数。
英文:
Math.random returns a number between 0 and 1.
So when cast to an int it will be always 0.
Select a scaling factor and multiply the result (lets say 5)
and then the result will an int in the range 0-4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论