Java-嵌套循环-无法获取任何输出
我提出这个想法是为了模拟植物的生长方式。这个想法是基于分形几何,以及分形几何如何解释植物中树枝和其他结构的重复。不过,我是一个编程新手,所以我想我应该测试一下重复某些东西的基本逻辑。下面是我用Java写的Java-嵌套循环-无法获取任何输出,java,if-statement,while-loop,Java,If Statement,While Loop,我提出这个想法是为了模拟植物的生长方式。这个想法是基于分形几何,以及分形几何如何解释植物中树枝和其他结构的重复。不过,我是一个编程新手,所以我想我应该测试一下重复某些东西的基本逻辑。下面是我用Java写的 /* Java program: plant.java This program runs a loop simulating plant growth. The idea is that the plant will continue to have "repeated" growth
/*
Java program: plant.java
This program runs a loop simulating plant growth.
The idea is that the plant will continue to have "repeated"
growth and that it will stop (or slow down to a crawl) at some point,
in this case at 100 branches.
*/
public class plant
{
public static void main (String [] poop)
{
int current_growth = 0, limit = 100; //plant growth starts at ZERO and is limited to 100
String word = "branches";
while (current_growth > 0)
{
//here, we are checking to see if the plant has grown just 1 inch.
//this is important just for grammatical purposes
if (current_growth == 1)
{
word = "branch"; //this changes the "word" to branch instead of branches
}
System.out.println("Your plant has grown " + current_growth + " " + word);
//if you put the singular count check here, it doesn't work at all
current_growth = current_growth + 1;
if (current_growth > 0)
{
System.out.println("Your plant has grown " + current_growth + " " + word);
}
else
{
System.out.println("Your plant will no longer grow.");
} // end else
} //end while loop
} //end main method
} //end class
我做了以下工作:
macbookair:Java-bdeely$Java-plant运行代码
macbookair:javabdeely$
我相当肯定我在这里犯了一些很大的新手错误。有人知道我如何让这段代码给我重复从0到100的循环的输出吗
谢谢 因为当前的增长被初始化为
0
while (current_growth > 0) // never becomes true
这是因为在开始时,您的
当前增长=0
,但当条件为(当前增长>0)
为什么不使用进行循环:
for(current_growth = 0; current_growth < limit; ++current_growth)
for(当前增长=0;当前增长
您当前的增长率为0。在while编辑中使其=0
:
public class plant
{
public static void main (String [] poop)
{
int current_growth = 0, limit = 1;//set limit to 1 //plant growth starts at ZERO and is limited to 100
String word = "branches";
while (limit<=100)// for 100 iterations limit
{
//here, we are checking to see if the plant has grown just 1 inch.
//this is important just for grammatical purposes
if (current_growth == 1)
{
word = "branch"; //this changes the "word" to branch instead of branches
}
System.out.println("Your plant has grown " + current_growth + " " + word);
//if you put the singular count check here, it doesn't work at all
current_growth = current_growth + 1;
if (current_growth > 0)
{
System.out.println("Your plant has grown " + current_growth + " " + word);
}
else
{
System.out.println("Your plant will no longer grow.");
} // end else
limit=limit+1;// increment
} //end while loop
} //end main method
} //end class
公共级工厂
{
公共静态void main(字符串[]poop)
{
int current_growth=0,limit=1;//将limit设置为1//植物生长从零开始,限制为100
String word=“分支”;
while(limitCheck你的while
循环条件。它不是真的,因此执行不会进入它。你可以使用并享受:)谢谢你的替代方法。我将使用for循环