这段代码的问题是在 beetleSimulation 下的 while 循环中,当 x/yCount 超出范围时,它会永远持续而不是退出。x 和 y 远远超过 20,谁能帮我解释一下为什么?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
void beetleSimulation(int, int)
;
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) // argc should be 2 for correct execution
{
// If the number of arguments is not 2
printf("%d", argc);
}
else
{
//run the bee
tle 模拟beetleSimulation(argv[1], argv[2] ); } }
void beetleSimulation(int size, int iterations){
int i;
int xCount = 0;
int yCount = 0;
int timeCount = 0;
int overallCount = 0;
for(i=0; i < 10; i++){
while(xCount < 20 || xCount > -20 || yCount <20 || yCount >-20){
timeCount += 1;
int degree = rand() % 360;
double radian = degree / 180 * PI;
xCount += sin(radian);
yCount += cos(radian);
}
//when beetle has died, add time it took to overall count, then go through for loop again
overallCount += timeCount;
}
//calculate average time
double averageTime = overallCount/iterations;
printf("%d",averageTime);
}