程序随机生成0-100之间的一个整数,由游戏者输入答案,程序根据答案及数字的大小输出提示语,游戏者根据提示继续输入,直到猜中为止
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
void main()
{
char c;
time_t begin, end;//所用的时间
int i; //数字
int guess;// 用户所猜的数字
srand((unsigned)time(NULL));//种子值
cout << "------------猜数游戏---------\n";
do
{
i = 0 + (int)(100 * rand() / (RAND_MAX + 0));//被猜数字
cout << "请输入你猜的数字(0到100之间):\n";
begin = time(NULL); //起始时间
cin >> guess;
while (guess != i)
{
if (guess > i)
cout << "你猜的数大了,请输入一个较小的数字: \n";
else
cout << "你猜的数小了,请输入一个较大的数字: \n";
cin >> guess;
}
cout << "恭喜你!您猜对了!\n";
end = time(NULL);
cout << "你一共花了" << difftime(end, begin) << "秒\n";
cout << "继续吗?(Y/N)\n";
cin >> c;
} while (c == 'Y' || c == 'y');
}