周一打卡

1. 问题描述:
编写程序,实现一个简单的猜数字游戏。程序随机生成一个 1~100 之间的整数,让玩家猜数字,直到猜中为止。

2. 设计思路:
程序需要用到随机数生成和输入输出。每次猜测后需要进行判断,判断猜测的数字与随机数的大小关系,提供相应提示。直到猜中为止,输出猜测的次数。

3. 程序流程图:

 

4. 代码实现:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
srand(time(NULL));
int ans = rand() % 100 + 1;
int times = 0;

while (true) {
int guess;
cout << "请输入猜测的数字(1~100):";
cin >> guess;
times++;

if (guess < ans) {
cout << "猜小了,请再猜一次!" << endl;
} else if (guess > ans) {
cout << "猜大了,请再猜一次!" << endl;
} else {
cout << "恭喜你,猜中了!总共猜了 " << times << " 次。" << endl;
break;
}
}

return 0;
}
posted @ 2023-04-17 18:21  菜鸟de博客  阅读(45)  评论(0)    收藏  举报