#include <math.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
const double ANSWER=3.14159;
double response;
printf("what is the value of pi?\n");
scanf("%lf",&response);
while(fabs(response-ANSWER)>0.0001)
{printf("Try again!\n");
scanf("%lf",&response);
}
printf("Close enough!\n");
return 0;
}
捕获.PNG
使用fabs()函数(声明在math.h)可以方便地比较浮点数,该函数返回一个浮点值的绝对值。
如果不适用fabs()函数while(response-ANSWER>0.0001)
捕获1.PNG