Menu
CoddyTech

用户输入

Coddy 使用 C++ 制作温度转换器 课程的第 9 课,共 10 课。

challenge icon

挑战

简单

在 main 函数中,在你当前的代码后面添加:

  1. 输出:Enter your choice (1-6):
  2. 获取用户输入,并将其保存到变量(choice)中。
  3. 输出:Enter the temperature:
  4. 获取用户输入,并将其保存到变量(temperature)中。

每个输出之间添加换行

亲手试试

#include <iostream>
using namespace std;

float celsiusToFahrenheit(float celsius) {
    return (celsius * 9.0 / 5.0) + 32.0;
}

double celsiusToKelvin(double celsius) {
    return celsius + 273.15;
}

double fahrenheitToCelsius(double fahrenheit) {
    return (fahrenheit - 32.0) * 5.0 / 9.0;
}

double fahrenheitToKelvin(double fahrenheit) {
    return (fahrenheit + 459.67) * 5.0 / 9.0;
}

double kelvinToCelsius(double kelvin) {
    return kelvin - 273.15;
}

double kelvinToFahrenheit(double kelvin) {
    return kelvin * 9.0 / 5.0 - 459.67;
}

int main() {
    cout << "Temperature Converter\n";
    cout << "1. Celsius to Fahrenheit\n";
    cout << "2. Celsius to Kelvin\n";
    cout << "3. Fahrenheit to Celsius\n";
    cout << "4. Fahrenheit to Kelvin\n";
    cout << "5. Kelvin to Celsius\n";
    cout << "6. Kelvin to Fahrenheit\n";
    
    return 0;
}

使用 C++ 制作温度转换器中的所有课

自己练习: C++ 在线编译器