gpt4 book ai didi

c - Printf 产生错误的整数

转载 作者:行者123 更新时间:2023-11-30 15:22:43 26 4
gpt4 key购买 nike

我学习 C 几天了,当我使用 Printf 转换温度时,它显示错误的输出。

int main() {
char sys;
int temp;
printf("Enter a tempature system [c or f]: ");
scanf("%c", &sys);
printf("Enter a tempature: ");
scanf("%s", &temp);
if (sys == 'f') {
int output = (temp - 32) * 5/9;
printf("%d Fahrenheit is %d Celsius\n",temp,output);
return 0;
}
else if (sys == 'c') {
int output = temp * 9/5 + 32;
printf("%d Celsius is %d Fahrenheit\n",temp,output);
return 0;
}
}

最佳答案

#include <stdio.h>

int main() {
char sys;
float temp,output;

printf("Enter a tempature system [c or f]: ");
scanf("%c", &sys);

printf("Enter a tempature: ");
scanf("%f", &temp);

if (sys == 'f') {
output = (temp - 32.0) * (5.0 / 9.0);
printf("%.2f Fahrenheit is %.2f Celsius\n",temp,output);
}
else if (sys == 'c') {
output = (temp * 9.0 / 5.0) + 32.0;
printf("%.2f Celsius is %.2f Fahrenheit\n",temp,output);
}

return 0;
}

关于c - Printf 产生错误的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com