美好的一天,我需要有关如何解决这个问题的提示

发布于 2025-01-18 07:20:29 字数 269 浏览 0 评论 0原文

使用 switch-case 编写一个 C++ 程序,要求用户输入华氏温度,然后程序将其转换为摄氏温度,并根据以下温度状态显示合适的消息:

Temp < > 0 然后冰冻天气

温度 1-10 然后非常冷天气

温度 11-20 然后寒冷天气

温度 21-30 然后正常温度 温度

31-40 然后它的热

温度 >=40 然后它非常热

我可以解决转换过程,但我不明白如何使用 switch case 来实现它,

Write a C++ program using switch-case that would ask the user to type a temperature in Fahrenheit then will be converted by the program in Celsius and will display a suitable message according to temperature state below:

Temp < 0 then Freezing weather

Temp 1-10 then Very Cold weather

Temp 11-20 then Cold weather

Temp 21-30 then Normal in Temp

Temp 31-40 then Its Hot

Temp >=40 then Its Very Hot

i can work around the conversion process, but i cannot understand how to implement it using switch case,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2025-01-25 07:20:29

switch-case 只能接受表达式和该表达式的结果。你可以用有点笨重的方式来做。但请记住,这种样式的可读性较低,实际上您最好使用简单的 if else 结构。

#include <iostream>
using namespace std;

int main()
{
    int temperature;
    cin >> temperature;
    switch(temperature)
    {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
            cout << "Very cold";
            break;
        case 11:
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
        case 19:
        case 20:
            cout << "Cold";
            break;
        case 21:
        case 22:
        case 23:
        case 24:
        case 25:
        case 26:
        case 27:
        case 28:
        case 29:
        case 30:
            cout << "Normal";
            break;
        case 31:
        case 32:
        case 33:
        case 34:
        case 35:
        case 36:
        case 37:
        case 38:
        case 39:
        case 40:
            cout << "Hot";
            break;
        default:
            if(temperature<0)
                cout << "Freezing";
            if(temperature>=40)
                cout << "Very hot";
    }
}

switch-case can accept only expression and result of this expression. You can do it in a bit bulky way. But keep in mind that this style is low-readable and actually you'd better use simple if else constructions.

#include <iostream>
using namespace std;

int main()
{
    int temperature;
    cin >> temperature;
    switch(temperature)
    {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
            cout << "Very cold";
            break;
        case 11:
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
        case 19:
        case 20:
            cout << "Cold";
            break;
        case 21:
        case 22:
        case 23:
        case 24:
        case 25:
        case 26:
        case 27:
        case 28:
        case 29:
        case 30:
            cout << "Normal";
            break;
        case 31:
        case 32:
        case 33:
        case 34:
        case 35:
        case 36:
        case 37:
        case 38:
        case 39:
        case 40:
            cout << "Hot";
            break;
        default:
            if(temperature<0)
                cout << "Freezing";
            if(temperature>=40)
                cout << "Very hot";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文