我正在为C编程分配,程序运行,但它似乎没有执行循环。该程序的目的是使用随机数生成器来获取变量,使用switch语句来确定气球的体积,然后计算出填充池所需的气球数量。但是,当我运行它时,它只是坐在那里,好像它正在等待输入,或者它无法退出循环。该程序应该以批处理模式运行。任何帮助将不胜感激!感谢。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define Gravity 32 //feet per second^2 and gravitational accelleration
#define PI 3.14159
#define BAL_HEIGHT 12 //balcony height in feet
#define POOL_CENTER 35 //distance to the center of the pool in feet
#define thetaMin 5 //degrees
#define thetaMax 85 //degrees
#define velocityMin 1 //foot per second
#define velocityMax 30 //feet per second
#define heightMin 4.5 //feet
#define heightMax 7.0 //feet
#define minDiameter 3 //inches
#define maxDiameter 9 //inches
#define Capacity 7 // pool capacity in gallons
int main ()
{
//inputs theta, velocity, height, diameter, balloons, volume, and capacity
//in degrees
//in feet per second
//of the thrower //for rand max/min
double currentCapacity = 0; //set beginning capacity to zero
double volume;
double volumeTotal = 0;
int diameter;
int balloonsThrown = 0;
int balloonsHit = 0; //set beginning balloons thrown and hit to zero
srand(time(NULL)); //seed sit using time
while (volumeTotal <= Capacity)
{
double theta = (double) rand()/RAND_MAX * (thetaMax - thetaMin +1) + thetaMin;
double velocity = (double) rand()/RAND_MAX * (velocityMin - velocityMax +1) + velocityMin;
double height = (double) rand()/RAND_MAX * (heightMin - heightMax +1) + heightMin;
int diameter = rand() % (maxDiameter - minDiameter + 1) + minDiameter;
int balloonsThrown = rand() % (RAND_MAX);
switch (diameter) // get diameter from rand equation
{
case 3:
volume == 0.1;//gallons
break;
case 4:
volume == 0.2;
break;
case 5:
volume == 0.3;
break;
case 6:
volume == 0.55;
break;
case 7:
volume == 0.8;
break;
case 8:
volume == 1.25;
break;
case 9:
volume == 1.7;
break;
}
// compute distance
double LAUNCH_HEIGHT = BAL_HEIGHT + height; //the sum of the input height and the balcony height
double radians = theta*(PI/180); //convert angle theta into radians
double part1 = (velocity*(cos(radians))/Gravity); //first part of the velocity equation
double part2 = velocity*(sin(radians)); //second part of velocity equation
double part3 = pow(velocity*(sin(radians)),2);//exponential part of the velocity equation
double part4 = 2*(Gravity*LAUNCH_HEIGHT);
double distance = part1*(part2 + sqrt(part3+part4)); //full distance function
//determine whether or not the balloon will fill the pool
//increment the amount of balloons thrown and that hit the pool
if (distance <= POOL_CENTER -1 && POOL_CENTER +1 >= distance);
{ // distance is in the range of 34-36 (Pool Center +- 1)
(currentCapacity += volumeTotal);
}
if (distance > 0);
{
(balloonsThrown ++);
}
//determining whether to add the balloon to the hit total
if (distance > POOL_CENTER -1 && distance < POOL_CENTER +1)
{
(balloonsHit ++);
}
}
double balloonPercentage = (balloonsHit/balloonsThrown);
printf ("%d balloons hit the pool.\n", balloonsHit);
printf ("%d balloons were thrown\n", balloonsThrown);
printf ("%2f%% of balloons hit the pool\n", balloonPercentage);
return 0;
}
答案 0 :(得分:2)
while (volumeTotal <= Capacity)
你永远不会改变volumeTotal
的值,所以这总是评估为0.0 <= 7
,这是真的并且循环继续。
答案 1 :(得分:0)
while(volumeTotal&lt; = Capacity) 几乎是一个无限循环,我想这是造成这个问题的原因,因为你的情况总是正确而且不会中断。
我认为你可以在开关中包含一个默认情况