代码加了循环以后输入数据没有反应,应该怎么处理?
#include <stdio.h>int Temper(double HS);
int main()
{
double i;
printf("请输入华氏温度:");
scanf("%lf",&i);
while(scanf("%lf",&i) == 1)
{
Temper(i);
printf("请输入华氏温度(q to quit):");
}
printf("程序结束!\n");
return 0;
}
int Temper(double HS)
{
double SS, KS;
SS = 5.0 / 9.0 * (HS - 32.0);
KS = SS + 273.16;
printf("华氏温度%.2lf,摄氏温度%.2lf,开氏温度%.2lf\n", HS, SS, KS);
}
代码加了while循环以后运行就出现了问题,一直卡在图中的情况,请问大佬们怎么解决? 你的while循环条件是要输入一次的
所以你上边那句输入语句就木得用了,注释掉
要是不注释掉你就得输入两次温度才能正常运行,而且第一次输入是木得用的 #include <stdio.h>
int Temper(double HS);
int main()
{
double i;
printf("请输入华氏温度:");
scanf("%lf",&i);
// while(scanf("%lf",&i) == 1)
// {
Temper(i);
// printf("请输入华氏温度(q to quit):");
// }
printf("程序结束!\n");
return 0;
}
int Temper(double HS)
{
double SS, KS;
SS = 5.0 / 9.0 * (HS - 32.0);
KS = SS + 273.16;
printf("华氏温度%.2lf,摄氏温度%.2lf,开氏温度%.2lf\n", HS, SS, KS);
}
这是正常运行的代码{:10_266:} #include <stdio.h>
int Temper(double HS);
int main()
{
double i;
printf("请输入华氏温度:");
//scanf("%lf",&i);
while(scanf("%lf",&i) == 1)
{
Temper(i);
printf("请输入华氏温度(q to quit):");
}
printf("程序结束!\n");
return 0;
}
int Temper(double HS)
{
double SS, KS;
SS = 5.0 / 9.0 * (HS - 32.0);
KS = SS + 273.16;
printf("华氏温度%.2lf,摄氏温度%.2lf,开氏温度%.2lf\n", HS, SS, KS);
}
另外没有返回值的话,用void 代替 int 比较好
稍作修改
#include <stdio.h>
void Temper(double HS);
int main() {
double i;
printf("提示:输入任意非数字结束输入!\n");
printf("请输入华氏温度:");
//scanf("%lf",&i);
while(scanf("%lf", &i) == 1) {
Temper(i);
printf("请输入华氏温度(q to quit):");
}
printf("程序结束!\n");
return 0;
}
void Temper(double HS) {
double SS, KS;
SS = 5.0 / 9.0 * (HS - 32.0);
KS = SS + 273.16;
printf("华氏温度%.2lf,摄氏温度%.2lf,开氏温度%.2lf\n", HS, SS, KS);
}
页:
[1]