注册 登录
编程论坛 C++教室

IF -else-条件语句问题请教

jmf6288 发布于 2018-06-29 22:59, 880 次点击
请帮忙看看以下作业题。谢谢!

考虑下面的代码片段,其中包含IF -否则IF -条件语句。重新编写代码,通过移除冗余的(不必要的)条件测试。

float income;
cout << "Enter your monthly income: ";
cin >> income;
if (income < 0.0)
cout << "Invalid." << endl;
else if (income >= 0.0 && income < 1200.00)
cout << "Poor." << endl;
else if (income >= 1200.00 && income < 2500.00)
cout << "Ok." << endl;
else if (income >= 2500.00)
cout << "Rich." << endl;
2 回复
#2
Jonny02012018-06-30 12:04
怎么看都只有最后一个可以改成else
其他实在想不到怎么优化
要不就把判断全部移到编译期进行
#3
rjsp2018-06-30 16:23
if (income < 0.0)
    cout << "Invalid." << endl;
else if (income < 1200.00)
    cout << "Poor." << endl;
else if (income < 2500.00)
    cout << "Ok." << endl;
else
    cout << "Rich." << endl;
1