0

我对如何完成这个 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 {}
     }

  }
4

5 回答 5

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时将停止循环,只是退出循环。

于 2013-09-06T17:18:07.943 回答
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 之前不要使用它。如果您确实想使用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);
     }
}
于 2013-09-06T17:21:25.360 回答
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);
     } 

 }
于 2013-09-06T17:28:33.977 回答
1

您可能想尝试使用 while 循环,以便不断提示问题,直到用户输入值 => 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);
    }
}
于 2013-09-06T17:44:28.687 回答
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;
     }
 }
于 2013-09-06T17:18:20.093 回答