gd6179 2018-06-30 00:27 采纳率: 100%
浏览 939
已采纳

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

IF -else-条件语句问题请教
请帮忙看看以下作业题。谢谢!

考虑下面的代码片段,其中包含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;

  • 写回答

11条回答 默认 最新

  • weixin_41841990 2018-06-30 07:54
    关注

    float income;
    cout << "Enter your monthly income: ";
    cin >> income;
    if (income < 0.0)
    cout << "Invalid." << endl;
    else if (income < 1200.00) //此内容在else之后,表明income已经>=0.0,故income >= 0.0可省略
    cout << "Poor." << endl;
    else if (income < 2500.00) //此内容在else之后,表明income已经>=1200.00,故income >= 1200.00可省略
    cout << "Ok." << endl;
    else //此内容在else之后,表明income已经>=2500.00,故可省略
    cout << "Rich." << endl;

    符合else的条件说明一定不符合if后的条件,故else后有些不必要的条件可以省略

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比