2024新版 黑马程序员《C++零基础入门》笔记——第二章0708 逻辑判断语句的嵌套及其习题讲解

1.逻辑判断语句的嵌套

2.代码实践

#include "iostream"
using namespace std;

/*
 * 如果小美喜欢我,我就去表白
 *      如果天气好,去踏青
 *      否则去看电影
 * 否则我去追求小新
 */

int main()
{
    int love;   // 1 表示喜欢  0 表示不喜欢
    int weather;    // 1 表示好天气 0表示不好的天气
    cout << "小美对我的态度是怎么样的?1是喜欢,0是不喜欢:" << endl;
    cin >> love;

    if (love)
    {
        // 小美喜欢我
        cout << "很高兴,小美喜欢我,我也喜欢你小美!" << endl;

        cout << "看一下天气,今天的天气怎么样?1好天气,0坏天气" << endl;
        cin >> weather;
        if (weather) {
            cout << "今天是个好天气,带小美一起踏青去。";
        } else {
            cout << "今天天气不好,去看电影吧!" << endl;
        }

    }else {
        // 小美不喜欢我
        cout << "很伤心,我去追求小新了!" << endl;
    }
    return 0;
}

3.作业

参考答案(以guess == num为条件)

#include "iostream"
using namespace std;


int main()
{
    int num = 5;
    int guess_num;
    cout << "请输入第一次猜想的数字:";
    cin >> guess_num;

    if (guess_num == num) {
        cout << "第一次就猜对了,你真棒!" << endl;
    } else {
        cout << "不对,请再猜一次:";
        cin >> guess_num;
        if (guess_num == num) {
            cout << "第二次猜对了,你真棒!" << endl;
        }else {
            cout << "不对,再最后猜一次:";
            cin >> guess_num;
            if (guess_num == num) {
                cout << "第三次猜对了,你真棒!" << endl;
            } else {
                cout << "Sorry,全部猜错啦,我想的是:" << num << endl;
            }
        }
    }

    return 0;
}

(以guess != num为条件)

# include "iostream"
using namespace std;

int main()
{
    int number = 5;
    int guess;
    cout << "请输入第一次猜想的数字:"<< endl;
    cin >> guess;

    if(guess != number){
        cout << "不对,请再猜一次:" << endl;
        cin >> guess;

        if (guess != number) {
            cout << "不对,再最后猜一次:" << endl;
            cin >> guess;

            if (guess != number) {
                cout << "sorry,全部猜错啦,我想的是:" << number << endl;
            } else {
                    cout << "第三次猜对了,你真棒!"<< endl;
            }
        } else {
            cout << "第二次猜对了,你真棒!" << endl;
        }
    } else {
        cout << "第一次就猜对了,你真棒!" << endl;
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值