我有一个小程序,可以将用户输入从华氏转换为摄氏。然后,它显示华氏温度从1太100与他们各自的摄氏对应物。但是当运行时,它输出这个。
华氏20度= -15度25华氏度= -15度30华氏度= -15度 华氏35度= -15度40华氏度= -15度45华氏度= -15度 华氏50度= -15度 华氏6度= -14度12华氏度= -14度18华氏度= -14度 华氏24度= -14度30华氏度= -14度36华氏度= -14度 华氏42度= -14度48华氏度= -14华氏度= -14华氏度=-14摄氏度 华氏60度= -14度 华氏7度= -13度14华氏度= -13度21华氏度= -13度 28华氏度= -13华氏度= -13华氏度= -13华氏度=-13华氏度=-13华氏度 华氏49度= -13度,华氏56度= -13度63华氏度= -13度 70华氏度= -13 Celcius 华氏8度= -13度16华氏度= -13度24华氏度= -13度 华氏32度= -13度40华氏度= -13度48华氏度= -13度 华氏56度= -13度64华氏度= -13度72华氏度= -13度 华氏80度= -13度 9华氏度= -12 Celcius 18华氏度= -12 Celcius 27华氏度= -12 Celcius 36华氏度= -12华氏度= -12华氏度= -12华氏度=-12华氏度=-12华氏度
这是我的密码。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
//Clear the screen
system("clear");
float celsius, fahrenheit;
printf("Please enter the temperature in Fahrenheit: \n");
scanf("%f", &fahrenheit);
//Converts the temperature from fahrenheit to celsius
celsius = (fahrenheit - 32) * 5 / 9;
printf("%.2f Fahrenheit = %.2f Celsius \n", fahrenheit, celsius);
printf ("-------------------------------- \n");
int i,j;
int num;
float c;
//Loop to create table
for(i=1; i<=10; i++)
{
num = i;
for(j=1; j<=10; j++)
{
c = (i*j-32) * 5 / 9;
printf("%3d Fahrenheit = %f Celcius \t",(i*j), c);
}
printf("\n");
}
return 0;
}发布于 2019-03-07 21:49:15
以下是问题和解决办法的总结:
确保对浮动/双倍使用正确的类型
建议你做一个功能:
double f2c(double f) {
return (f - 32.0) * 5/9;
}
int main() {
printf("%g°F = %g°C\n", 100.0, f2c(100.0));
}从0数到99
对于(i= 0;i< 100;i++) {}
漂亮的打印0-99每一个新行为10s
对于(i= 0;i< 100;i++) { print("%3d%1c“),i,(1+i) % 10‘’:'\n');}
把所有的东西都放在一起
#include <stdio.h>
double f2c(double f) {
return (f - 32) * 5/9;
}
int main() {
int i;
for(i = 0; i < 213; i++) {
printf("%3d°F => %3.0f°C%s", i, f2c(i), (1+i) % 10? " | " : "\n");
}
}https://stackoverflow.com/questions/55052815
复制相似问题