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

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后有些不必要的条件可以省略

    点赞 评论
  • 一脸滑稽 2018-06-30 00:40
    float income;
    
    cout << "Enter your monthly income: ";
    cin >> income;
    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;
    
    点赞 评论
  • clc_vi 2018-06-30 00:59

    float income;
    count << "Enter your monthly income:";
    if (income >=2500.00)
    cout << "Rich." << endl;
    else if (income >= 1200.00)
    cout << "Ok." << endl;
    else if (income >= 0.0)
    cout << "Poor." << endl;
    else
    cout << "Invalid." << endl;

    //就用一边来比较就可以了

    点赞 评论
  • ambit_tsai-微信 2018-06-30 01:46
    float income;
    cout << "Enter your monthly income: ";
    cin >> income;
    
    if (income < 0.0)
        cout << "Invalid." << endl;
    else if (income < 1200.00)  // 若程序运行到这,则上个判断语句为假,显然income不小于0.0,所以再判断income>=0.0是多余的
        cout << "Poor." << endl;
    else if (income < 2500.00)  // 同上
        cout << "Ok." << endl;
    else    // 同上
        cout << "Rich." << endl;
    
    点赞 评论
  • june135790 2018-06-30 03:02

    float income;
    cout << "Enter your monthly income: ";
    cin >> income;

    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;
            }
        }
    }
    
    点赞 评论
  • 努力|奋斗 2018-06-30 07:47

    string info[4] = { "Invalid.","Poor.", "Ok.","Rich." };
    float income;
    count << "Enter your monthly income:";
    cin >> income;

    int a = (income > 0) + (income > 1200) + (income > 2500);
    cout << info[a] << endl;

    点赞 评论
  • zhugx001 2018-06-30 08:04

    float income;
    cout << "Enter your monthly income: ";
    cin >> income;
    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;

    点赞 评论
  • gd6179 2018-06-30 10:11

    感谢各位老师。谢谢!

    点赞 评论
  • Flao 2018-06-30 12:06

    elseif语句 一般都是分支 类似于switch case

    点赞 评论
  • csdn_playboy 2018-07-01 05:53

    float income;
    cout << "Enter your monthly income: ";
    cin >> income;

    if (income < 0.0)
    cout << "Invalid." << endl;
    else if (income < 1200.00) // 若程序运行到这,则上个判断语句为假,显然income不小于0.0,所以再判断income>=0.0是多余的
    cout << "Poor." << endl;
    else if (income < 2500.00) // 同上
    cout << "Ok." << endl;
    else // 同上
    cout << "Rich." << endl;

    点赞 评论
  • qq_41949708 2018-07-02 02:44

    float income;
    count << "Enter your monthly income:";
    if (income >=2500.00)
    cout << "Rich." << endl;
    else if (income >= 1200.00)
    cout << "Ok." << endl;
    else if (income >= 0.0)
    cout << "Poor." << endl;
    else
    cout << "Invalid." << endl;

    //就用一边来比较就可以了

    点赞 评论

相关推荐 更多相似问题