循环重复时,为什么随机值不会分配给特定变量而发生变化?

huangapple go评论80阅读模式
英文:

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(&quot;Composition Statistics for Families with Two Children: \n&quot;);
System.out.println(&quot;Total Number of Families: &quot;);
FamilyNumber = Integer.parseInt(in.nextLine());
List&lt;String&gt; list = Arrays.asList(boy, girl);
while (RunCount &lt; 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 == &quot;BG&quot; || GenderValues == &quot;GB&quot;) {
BGCount++;
}
else if (GenderValues == &quot;GG&quot;) {
GGCount ++;
}
else {
BBCount++;
}
RunCount++;
}
GGPercent = ((double)(GGCount/FamilyNumber)*(100));	
BGPercent = ((double)(BGCount/FamilyNumber)*(100));
BBPercent = ((double)(BBCount/FamilyNumber)*(100));
System.out.println(&quot;Number of Families with: \n&quot;);
System.out.println(&quot;\tTwo Boys: &quot; + BBCount + &quot; Represents &quot; + BBPercent + &quot;%&quot;);
System.out.println(&quot;\tTwo Girls: &quot; + GGCount + &quot; Represents &quot; + GGPercent + &quot;%&quot;);
System.out.println(&quot;\tOne Boy and One Girl: &quot; + BGCount + &quot; Represents &quot; + BGPercent + &quot;%&quot;);

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

huangapple
  • 本文由 发表于 2020年10月25日 01:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64516346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定