对于C-实现中的循环

时间:2013-09-06 17:15:10

标签: c for-loop

我对如何完成for循环感到困惑。任务是读取unix中的输入。对于输入,如果半径> 0,则应每次提示用户,然后如果< = 0则应终止。我从厘米到平方英寸。我的当前配置在输出到控制台之前需要2个输入(1个提示,1个不输入)。欢呼声。

#include <stdio.h>
#define PI 3.14159

main()
{
float r, a;
  int y = 9999999;


  for(int i =0; i <y; i++){
     printf("Enter the circle's radius (in centimeters): ");
     scanf ("%f", &r);

        if(r>0){
           r=r;
       a = PI * r * r *2.54;

       printf("Its area is %3.2f square inches.\n", a);
   }  else {}
     }

  }

5 个答案:

答案 0 :(得分:2)

您的代码流如下:

for (infinite condition) {
  scan input
  if (input > 0) {
    do things
  }
  else {
    do nothing
  }
}

所以没有办法退出循环,这就是break语句存在的原因,强制退出迭代的代码块:

while (true) {
  scanf ("%f", &r);
  if (r > 0) {
    // do whatever;
  }
  else
    break;
}

break将在执行时停止循环,只是退出循环。

答案 1 :(得分:1)

考虑使用while loop

#include <stdio.h>
#define PI 3.14159

main(){
    float r, a;

    int continueBool = 1;
    while(continueBool == 1){
         printf("Enter the circle's radius (in centimeters): ");
         scanf ("%f", &r);

         if(r>0){
              a = PI * r * r *2.54;
              //the above formula may be wrong, so consider trying:
              //a = PI * r * r/2.54/2.54;
              printf("Its area is %3.2f square inches.\n", a);
         }
         else{
             continueBool = 0;
         }
     }
}

如果您不熟悉C编程,break语句 会很危险,因此我建议使用它,直到您更好地了解C和打破。如果你想要使用break,那么这可能是你的解决方案:

#include <stdio.h>
#define PI 3.14159

main(){
    float r, a;

    while(1){
         printf("Enter the circle's radius (in centimeters): ");
         scanf ("%f", &r);

         if(r<=0){
             break;
         }

         a = PI * r * r *2.54;
         //the above formula may be wrong, so consider trying:
         //a = PI * r * r/2.54/2.54;
         printf("Its area is %3.2f square inches.\n", a);
     }
}

答案 2 :(得分:1)

r=1.0f;

// break if no. of cases exhausted or r is negative or zero
for(int i =0; i < y && r > 0; i++) 
{
     printf("Enter the circle's radius (in centimeters): ");

     if( scanf ("%f", &r) == 1) // Always check for successful scanf
     {
          a = PI * r * r/2.54/2.54; //This is correct formula

          printf("Its area is %3.2f square inches.\n", a);
     } 

 }

答案 3 :(得分:1)

您可能希望尝试使用while循环,以便在用户输入值=&gt; 0之前不断提示问题。看看下面是否有帮助(你的转换因子也不太正确);

#include <stdio.h>
#define PI 3.14159

void main()
{
    float r, a;
    printf("Enter the cirle's radius (in centimeters):");
    scanf("%f",&r); 

    while (r>0)
    {
        a=PI*r*r*0.155; // conversion from sqcm to sqin is ~0.155
        printf("Its area is %3.2f square inches \n", a);
        printf("Enter the cirle's radius (in centimeters):");
        scanf("%f",&r);
    }
}

答案 4 :(得分:0)

使用此:

for(int i =0; i < y; i++)
{
     printf("Enter the circle's radius (in centimeters): ");
     scanf ("%f", &r);

     if(r > 0)
     {
          a = PI * r * r *2.54;

          printf("Its area is %3.2f square inches.\n", a);
     } 
     else
     {
          break;
     }
 }