Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一个while循环中,如何跟踪一个循环完成了多少次迭代?_Java_Loops_Foreach_While Loop_Increment - Fatal编程技术网

Java 在一个while循环中,如何跟踪一个循环完成了多少次迭代?

Java 在一个while循环中,如何跟踪一个循环完成了多少次迭代?,java,loops,foreach,while-loop,increment,Java,Loops,Foreach,While Loop,Increment,我试图使这段代码达到while循环只对foreach循环进行50次迭代,或者直到hasAChildFoundMother为true为止 我需要声明一个用于跟踪迭代次数的变量,该变量将是while循环条件的一部分;此外,该变量需要在while循环内部递增,但在foreach循环外部递增 public void findMother() { Random generator = new Random(); while (!hasAChildFoundMothe

我试图使这段代码达到while循环只对foreach循环进行50次迭代,或者直到hasAChildFoundMother为true为止

我需要声明一个用于跟踪迭代次数的变量,该变量将是while循环条件的一部分;此外,该变量需要在while循环内部递增,但在foreach循环外部递增

public void findMother() {

         Random generator = new Random();

         while (!hasAChildFoundMother()) {

             for (Turtle babyTurtles : this.babyTurtles) {
                 double degrees = generator.nextInt(90) + 1;
                 babyTurtles.turn(degrees);

                 int distance = generator.nextInt(50 - 10) + 10;
                 babyTurtles.forward(distance);
             }

        }


    }
我真正遇到的问题是如何“在while循环内但在foreach循环外”增加它

public void findMother(){
随机生成器=新随机();
int i=0;

while(!hasAChildFoundMother()&&i这似乎是使用
for
循环而不是
while
循环的理想情况:

for(int i = 0; i < 50 && !hasAChildFoundMother(); i++) {
   for (Turtle babyTurtles : this.babyTurtles) {
      ...
   }
}

这个问题有点让人困惑,但我认为应该这么简单:

声明
count
变量并在while循环中添加
count++
。然后设置一个条件,使count停止在50。

public void findMother(){
public void findMother() {
     int counter = 0;
     Random generator = new Random();

     while ((!hasAChildFoundMother()) && (counter < 50)) {
         counter++;
         for (Turtle babyTurtles : this.babyTurtles) {
             double degrees = generator.nextInt(90) + 1;
             babyTurtles.turn(degrees);

             int distance = generator.nextInt(50 - 10) + 10;
             babyTurtles.forward(distance);
         }

    }


}
int计数器=0; 随机生成器=新随机(); 而((!hasAChildFoundMother())&&(计数器<50)){ 计数器++; 对于(海龟宝宝尾巴:这个。宝宝尾巴){ 双度=发电机。下一个(90)+1; 婴儿荨麻疹。转(度); int距离=发电机。nextInt(50-10)+10; 婴儿荨麻疹。向前(距离); } } }
所以基本上你要计算你运行while循环的次数,每次你开始一个新的循环时,它会检查你是否完成了50次迭代,如果是的话,它就会完成


注意,重要的是(计数器<50)(计数器在while循环之前初始化,在while条件下进行测试,在while循环内部但在for循环外部递增(即,将其粘贴在带有
for
的代码行之前,但在带有
while
的代码行之后)。来这里之前试试谷歌。谢谢。我来这里之前“谷歌”过这个,但有时事情对某些人来说并不那么容易。我知道我应该做什么,但我只是想确定一下。谢谢你在这件事上表现得像个傻瓜。哈哈,你找到合适的答案了吗?你能留下评论和/或接受答案吗?
int i = 0;
while (i < 50 && !hasAChildFoundMother()) {
   for (Turtle babyTurtles : this.babyTurtles) {
      ...
   }
   i++;
}
public void findMother() {
     int counter = 0;
     Random generator = new Random();

     while ((!hasAChildFoundMother()) && (counter < 50)) {
         counter++;
         for (Turtle babyTurtles : this.babyTurtles) {
             double degrees = generator.nextInt(90) + 1;
             babyTurtles.turn(degrees);

             int distance = generator.nextInt(50 - 10) + 10;
             babyTurtles.forward(distance);
         }

    }


}