c++猜数字

#include<iostream>
using namespace std;
void myGuess() {
    int s = rand() % 200 + 1;//利用取余随机生成1~200的数字
    int c;
    cout << "你猜测的数是(1~200)" << endl; 
    int t = 0;//猜测的次数
    while (t < 10) {
        cin >> c;
        if (s==c) {
            cout << "猜对了,游戏结束!" << endl;
            cout << "你一共猜了" << t + 1 << "次" << endl; break;//因为t++在循环后面,因此这个t需要加上1
        }
        else {
            if (c < s)
                cout << "猜错了,太小了" << endl;
        } {if (c > s)
            cout << "猜错了,太大了" << endl; }
        t++;
        if (t == 10)
            cout << "你的机会已用完,游戏结束!" << endl;
        else
            cout << "你还有" << 10 - t << "次机会" << endl;
        continue;
    }
}
int main() {
    srand((unsigned int)time(NULL));//创建随机种子  利用当前系统时间生成随机数,防止每次随机数都一样
    cout << "**************************" << endl;
    cout << "********1、开始游戏*******" << endl;
    cout << "********0、结束游戏*******" << endl;
    cout << "**************************" << endl;
    int select;
    cout << "请输入你的选择:" << endl;
    cin >> select;
    switch (select) {
    case 1:myGuess();
        return 0;
    case 0:cout << "退出成功!" << endl;
        exit(0);
    default:cout << "输入有误,请重新输入!" << endl; break;
    }
    return 0;
}
 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值