【问题标题】:Cannot exit loop, run program successfully无法退出循环,成功运行程序
【发布时间】:2013-09-26 17:35:43
【问题描述】:

我正在处理 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; 
     } 

【问题讨论】:

    标签: random while-loop switch-statement


    【解决方案1】:
    while (volumeTotal <= Capacity) 
    

    您永远不会更改 volumeTotal 的值,因此它的计算结果始终为 0.0 &lt;= 7,这是真的并且循环继续。

    【讨论】:

      【解决方案2】:

      while (volumeTotal

      我也认为你可以在 switch 中包含一个默认情况

      【讨论】:

        猜你喜欢
        • 2014-10-31
        • 2017-06-14
        • 2023-02-23
        • 2020-11-23
        • 2016-06-01
        • 2020-06-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多