| 代码:
 /*
 *   功    能:猜数字的游戏
 *   编译环境:windows2000 + Dev-c++ 4.9
 *   作    者:jhkdiy
 *   电子邮件:jhkdiy_gzb@21cn.net
 *   备    注:无意中看到《Beginning C++ game programming》一书,随便浏览了一下,
 *             觉得这个游戏有点意思,所以自己完善了下。初学者用来学习一下语法
 *             和提高一下兴趣还是很有意思的。该程序有BUG,大家找出来,
 *             不是语法错误哦!!思考一下!
 */
 
 #include <cstdlib>
 #include <iostream>
 #include <ctime>
 
 using namespace std;
 
 int main(int argc, char *argv[])
 {
 //产生随机数种子
 srand(time(0));
 
 int  theNumber = rand() % 100 + 1;     //随机数控制在1-100之间
 int  tries = 0,                        //用户尝试的次数
 guess;                            //用户输入的数字
 char bPlayAgain;                       //是否继续游戏
 
 cout << "\tWelcome to guess my number\n\n";
 
 do
 {
 //接受用户的输入
 cout << "Enter a guess: ";
 cin >> guess;
 ++tries;
 
 //如果输入的数字大于产生的随机数
 if( guess > theNumber )
 {
 cout<<"Too high!\n\n";
 }
 
 //如果输入的数字小于产生的随机数
 if( guess < theNumber )
 {
 cout << "Too low!\n\n";
 }
 
 //猜对了・・・
 if( guess == theNumber )
 {
 cout << "\n\n\tVery good! you get it!"
 << "\tThe number is: " << theNumber << endl;
 
 cout << "\n\n\tyou try " << tries << " times!" << endl;
 
 //是否继续游戏
 cout << "\n\nDo you want to play again?(y/n)";
 cin  >> bPlayAgain;
 if( bPlayAgain == 'y' || bPlayAgain == 'Y')
 {
 //清屏后继续游戏
 system("cls");
 continue;
 }
 else if( bPlayAgain == 'n' || bPlayAgain == 'N')
 {
 cout << "\nSee you next time, bye!\n";
 break;
 }
 else
 {
 cout << "\nEnter a wrong choice! program will exit!\n";
 break;
 }
 }
 
 }while(true);
 
 //退出程序
 system("pause");
 return EXIT_SUCCESS;
 }
 
 
 
 
 |