文章目录[隐藏]
c++++实现猜数字游戏代码
#include<iostream> #include<time.h> using namespace std; void main() { int y, r, a,k=0; cout << " -------------------------------------------------------------------------" << endl; cout << " **** ^_^ 你好,欢迎来到猜数字游戏 ^_^ *****" << endl; cout << " ------------------------------------------------------------------------" << endl; cout << " **** 如果想了解游戏规则请按1,直接进入请按0 *****" << endl; cout << " ------------------------------------------------------------------------" << endl; cout << " **** 李大叔吖猜数字游戏1.0版本 *****" << endl; cout << " ------------------------------------------------------------------------" << endl; cin >> y; if (y) { cout << " *-*-*-*-*-*-*-*-*-* 游戏规则 *-*-*-*-*-*-*-*-*-*-*" << endl; cout << " | 您要输入四位数字,程序会随机产生一个随机数如: 若电脑数字 |" << endl; cout << " * 是1234,而你输入的是1439,则会提示您输入的数字过大哦! *" << endl; cout << " | 若你输入的是1111,则会提示您输入的数字过小哦!一直到您猜对这个数字|" << endl; cout << " *---------------------------------------------------------------------*" << endl; cout << " | 欢迎进入游戏 |" << endl; cout << " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl; } while (y != -1) { srand((unsigned)time(NULL)); r = 999 + (rand() % 1000); cout << r << endl; cout << "欢迎使用李大叔吖猜数字程序" << endl<< "现在我这有一个四位数" << endl << "请你来猜猜这个数是多少 ? " << endl << "告诉我此刻你脑海中浮现的那个数。" << endl; cin >> a; while (r != a) { for (k = 0; r != a; k++) { if (r > a) cout << "你输入的数字过小了哦!请重新输入哦!" << endl; else cout << "你输入的数字过大了哦!请重新输入哦!" << endl; cin >> a; } } cout << "恭喜你,猜对了!本次的数字为" << r << "!!" << endl; cout << "----------------------------------" << endl; cout << "congratulation!您一共猜了" << k << "次" << endl; cout << "----------------------------------" << endl; cout << "系统给您的评价是:" << endl; if (k < 4) cout << "神一般的猜测!" << endl; if (k > 3 && k < 11) cout << "你真是太厉害了!" << endl; if (k > 10 && k < 21) cout << "还不错,继续努力!" << endl; if (k > 20) cout << "你太逊了,猜了这么多次!" << endl; cout << "您希望继续猜数字游戏吗?(输入-1退出 输入0继续)" << endl; cin >> y; } system("pause"); } wwwgaodaimacom