Java中的无限循环问题
当我在这段代码的底部运行 for
循环时,我得到一个无限循环,每次使用 i
if 语句中的输出> 每次都会增加并且不会停止。我很确定这是一个小问题,我只是无法指出......
import java.util.Scanner;
public class HW2johnson_pp1 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the species with the higher" +
" population first\n");
System.out.printf("Enter the name of your first species ");
String Species1 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop1 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth1 = keyboard.nextInt();
System.out.printf("Enter the name of your second species: ");
String Species2 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop2 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth2 = keyboard.nextInt();
if (Pop2 > Pop1) {
System.out.printf("The first population must be higher. \n");
System.exit(0);
}
Species input1 = new Species();
input1.name = Species1;
input1.population = Pop1;
input1.growthRate = Growth1;
Species input2 = new Species();
input2.name = Species2;
input2.population = Pop2;
input2.growthRate = Growth2;
if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <=
(input1.predictPopulation(2) - input2.predictPopulation(2))){
System.out.printf(Species2 + " will never out-populate " +
Species1 + "\n");
}
else {
for (int i = 0; input2.predictPopulation(i) <=
input1.predictPopulation(i); i++) {
if (input2.predictPopulation(i) == input1.predictPopulation(i)) {
System.out.printf(" will out-populate \n");
}
}
}
}
}
public int predictPopulation(int years)
{
int result = 0;
double populationAmount = population;
int count = years;
while ((count > 0) && (populationAmount > 0))
{
populationAmount = (populationAmount +
(growthRate / 100) * populationAmount);
count--;
}
if (populationAmount > 0)
result = (int)populationAmount;
return result;
}
when I run the for
loop towards the bottom of this code I get an infinite loop that outputs the output in the if
statement every single time with i
being incremented each time and doesn't stop. I'm pretty sure it's a small problem that I just can't put my finger on...
import java.util.Scanner;
public class HW2johnson_pp1 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the species with the higher" +
" population first\n");
System.out.printf("Enter the name of your first species ");
String Species1 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop1 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth1 = keyboard.nextInt();
System.out.printf("Enter the name of your second species: ");
String Species2 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop2 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth2 = keyboard.nextInt();
if (Pop2 > Pop1) {
System.out.printf("The first population must be higher. \n");
System.exit(0);
}
Species input1 = new Species();
input1.name = Species1;
input1.population = Pop1;
input1.growthRate = Growth1;
Species input2 = new Species();
input2.name = Species2;
input2.population = Pop2;
input2.growthRate = Growth2;
if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <=
(input1.predictPopulation(2) - input2.predictPopulation(2))){
System.out.printf(Species2 + " will never out-populate " +
Species1 + "\n");
}
else {
for (int i = 0; input2.predictPopulation(i) <=
input1.predictPopulation(i); i++) {
if (input2.predictPopulation(i) == input1.predictPopulation(i)) {
System.out.printf(" will out-populate \n");
}
}
}
}
}
public int predictPopulation(int years)
{
int result = 0;
double populationAmount = population;
int count = years;
while ((count > 0) && (populationAmount > 0))
{
populationAmount = (populationAmount +
(growthRate / 100) * populationAmount);
count--;
}
if (populationAmount > 0)
result = (int)populationAmount;
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这:
没有道理。显然,您的目的是“交换”两个总体的值,但相反,您只是使两个总体相同,因此对于任何
years
值,predictPopulation(years )
对于两个对象来说是相同的。编辑添加:此测试:
不是查看物种 2 最终是否会超过物种 1 的有效方法。如果物种 2 具有更高的比例增长率(
GrowthRate
),那么它最终将超过物种 1,即使在最初几年,它的绝对增长率较低。 (这仅与您的无限循环无关 - Dampsquid 指出,如果您' d 有<=
而不是<
— 但这是您需要解决的问题。)This:
does not make sense. Apparently what you intend is to "swap" the values for the two populations, but instead, you just cause the two populations to be identical, so that for any value of
years
,predictPopulation(years)
will be the same for both objects.Edited to add: This test:
is not a valid way to see whether species 2 will eventually overtake species 1. If species 2 has a greater proportional growth rate (
growthRate
), then it will eventually overtake species 1, even if during the first few years, it has a lower absolute growth rate. (This only tangentially relates to your infinite loop — Dampsquid points out that you wouldn't have had the infinite loop if you'd had<=
instead of<
— but it's a problem you need to fix.)你的问题在这里
input1 的人口永远不会少于 input2,这与以下情况之一有关:
input1 正在获取一个参数值,该值不允许其“人口”少于 input2(检查输入范围以确保这种情况可能会发生,例如确保 input2 的增长率 > input1)
类本身有问题,强制 input1 始终等于或大于 input2。
编辑:既然您已经发布了 PredictPopulation 的代码,它看起来就像是第一。确保输入 2 的增长率始终大于输入 1。
Your issue is here
The population of input1 is never becoming less then input2 this has to do with one of the following:
input1 is getting a parameter value that would not allow it's "population" to become less then input2 (check ranges of inputs to make sure this can occur, maybe something like making sure that input2's growth rate is > input1)
There is something wrong with the class itself that is forcing input1 to always be equal to or greater then input2.
EDIT: since you have posted the code for predictPopulation it looks like it is number one. Make sure that your growth rate for input2 is always greater then input1.