猜数字游戏

2025-01-19 14:19:44
推荐回答(1个)
回答1:

#include
#include
using namespace std;
int ans(int start,int end) //生成随机数的函数,start和end是他的范围
{
return start+(end-start)*rand()/(RAND_MAX+1);
}
int main()
{
srand(time(0));
char c;
int n=1,input,NOguess;
cout<<"I have a number between 1 and 1000."< cout<<"Can you guess my number? "< NOguess=ans(1,1000);//生成随机数
do{
cout<<"Please type your first guess."< while (cin>>input) {//输入猜的数据
//if(input==-1){cout<<"The number is "< //如果想在中途知道数值是什么,加这段代码进去,输入-1就知道在猜的数据
if(input==NOguess){cout<<"Excellent!You guessed the number! "< if(input cout<<"Too low.Try again."< if(input>NOguess)
cout<<"Too high.Try again."< }
cout<<"Would you like to play again (y or n)?"< cin>>c;
}while(c=='y'||c=='Y');
return 0;
}