C编程-温度转换问题

标签 c equation temperature

书中的问题:

编写一个将华氏温度转换为摄氏温度的程序,您的程序应该:

  1. 提示用户想要进行哪种类型的转化。
  2. 提示用户输入想要转换的温度。

我得到的输出不正确。我不确定哪里出了问题。我是 c 语言新手。任何帮助是极大的赞赏。 这是我的代码:

  #include <stdio.h>
  #include <stdlib.h>


 int main()
 {int f, c, f_or_c;

 printf("Would you like to convert Fahrenheit (1) or Celsius (2)?\n");
 scanf("%d", &f_or_c);

 if(f_or_c==1)
 {
    printf("Enter the temperature in Fahrenheit to convert?\n");
    scanf("%d", &c);
    f = 1.8*c + 32.0;
    printf("Celsius of %d is %d degrees.\n");

 }
 if(f_or_c==2)
 {
    printf("Enter the temperature in Celsius to convert?\n");
    scanf("%d", &f);
    c = (f-32)*5/9;
    printf("Fahrenheit of %d is %d degrees.\n");
 }
 return 0;
 }

最佳答案

我的猜测是您只是没有打印出值,但其他一切看起来都不错。

printf("Fahrenheit of %d is %d degrees.\n");

您没有打印任何变量。

这可能对你有用

printf("Fahrenheit of %d is %d degrees.\n", f, c); 

您可以在此处查看 printf 的一般用法 http://www.cplusplus.com/reference/cstdio/printf/

关于C编程-温度转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24501527/

相关文章:

c - 字符串排序与合并排序

c++ - 如何找到激活时打开给定 HMENU 的菜单项(如果有)?

c - 为什么在 c 中打印字符串数组不提供相同的输出?

python - 尝试求解隐式方程时出现 UnboundLocalError 'referenced before assignment'

C++如何扩展/因式分解方程(不求解)

matlab符号方程给出未确定的结果?

将 5 个温度从华氏度转换为摄氏度

c++ - 温度转换程序c++

无法使用结构在 C 数据文件中存储整数值

c++ - 获取 CPU 温度