美好的一天,我需要有关如何解决此问题的提示
good day i need tips on how to work around this problem
使用 switch-case 编写一个 C++ 程序,要求用户输入华氏温度,然后程序将转换为摄氏温度,并根据下面的温度状态显示合适的消息:
温度 < 0 然后结冰天气
温度 1-10,然后是非常寒冷的天气
温度 11-20 然后寒冷天气
温度 21-30 然后正常温度
温度 31-40 然后很热
温度 >=40 然后非常热
我可以绕过转换过程,但我不明白如何使用 switch case 来实现它,
switch-case
只能接受表达式和该表达式的结果。你可以用有点笨重的方式来做。但请记住,这种风格是 low-readable 实际上你最好使用简单的 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 编写一个 C++ 程序,要求用户输入华氏温度,然后程序将转换为摄氏温度,并根据下面的温度状态显示合适的消息:
温度 < 0 然后结冰天气
温度 1-10,然后是非常寒冷的天气
温度 11-20 然后寒冷天气
温度 21-30 然后正常温度
温度 31-40 然后很热
温度 >=40 然后非常热
我可以绕过转换过程,但我不明白如何使用 switch case 来实现它,
switch-case
只能接受表达式和该表达式的结果。你可以用有点笨重的方式来做。但请记住,这种风格是 low-readable 实际上你最好使用简单的 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";
}
}