要求输入q或其他非数字值时,循环结束。
参考大佬答案
https://blog.csdn.net/kexiaojiu/article/details/50173149
得知将scanf函数和数据类型联系在一起的方式——scanf函数的返回值。
见书p81:
scanf()函数返回成功读入项目的个数。
如果它没有读取任何项目(当它期望一个数字而您却键入了一个非数字字符串时就会发生这种情况),scanf()回返回0。
当它检测到“文件结尾”(end of file)时,它会返回EOF(-1)。
答案很清晰了,读书还是要认真哦。
最后附上我的代码。
#include "stdio.h"
void Temperatures(double f);
int main(void){
double fahrenheit;
int judge = 1;
printf("enter a farenheit:");
scanf("%lf",&fahrenheit);
while (judge == 1) {
Temperatures(fahrenheit);
printf("then enter next fahrenheit(q to quit):");
judge = scanf("%lf",&fahrenheit);
}
printf("bye\n");
return 0;
}
void Temperatures(double f){
double celsius;
double kelvin;
celsius = 1.8 * f + 32;
kelvin = celsius + 273.16;
printf("the fahrenheit = %.2f\n",f);
printf("the celsius = %.2f\n",celsius);
printf("the kelvin = %.2f\n",kelvin);
//系数题目要求要用常量定义,我偷懒了
}
·```