c++华氏或摄氏转换器

c++ Fahrenheit or Celsius converter

本文关键字:转换器 c++      更新时间:2023-10-16

嗨,只是想知道为什么这是不工作,截至目前,它只打印出华氏温度,无论我选择c或f,请帮助

#include <iostream>
using namespace std;
int main()
{
    char unit;
    float degrees = 0.0;
    float Farenheit, Celsius;
   cout << "Enter the temperature unit you are currently in (f or c): ";
   cin >> unit;
   cout << "Enter the temperature in degrees: ";
   cin >> degrees;
   if ( unit == 'c' || 'C')
   {
      Farenheit = (degrees - 32) / 9 * 5.0;
      cout << "The degrees in Farenheit are: " << Farenheit << endl;
   }
   else if ( unit == 'f' || 'F')
   {
      Celsius = (degrees - 32) * 5.0/9;
      cout << "The degrees in Celsius is: " << Celsius << endl;
   }
   return 0;
   }

这样可能效果更好:

if ( unit == 'c' || unit == 'C')