本文共 786 字,大约阅读时间需要 2 分钟。
猜数字游戏
#include#include #include using namespace std;int main() { int n, gn; int m = 0; srand((unsigned)time(0)); n = rand() % 1000 + 1; for(;;) { cout << "Please input a number:"; cout << endl; cin >> gn; m++; if(gn > n) { cout << "大了" << endl; } else if(gn < n) { cout << "小了" << endl; } else { cout << "猜对了!" << endl; cout << "一共猜了" << m << "次" << endl; break; } return 0; }}
这个程序是一个简单的猜数字游戏,用户需要通过输入猜测数字来猜出随机生成的1000以内的数字。程序会根据猜测结果提示用户“大了”或“小了”,直到用户猜对为止,并统计猜测的次数。
程序的主要功能包括:
这个程序的设计简单直观,用户交互性较强,适合用于学习或娱乐目的。
转载地址:http://wcgnz.baihongyu.com/