我一般生成随机数的方法就是srand(int (time(NULL));int 变量名=rand()%范围+1;
这次用这种方法来做一个小游戏。
因为非常简单,所以直接把代码贴出来:
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main() {
bool flag=true;
while(flag) {
srand(int(time(NULL)));
int cs, fw, zq;
cout<<"请输入猜的次数: ";
cin>>cs;
cout<<"\n请输入随机数范围: "<<endl;
cin>>fw;
zq=rand()%fw+1;
for(int i=1; i<=cs; i++) {
int cx;
bool a=false;
printf("输入第%d次猜测的数: ", i);
cin>>cx;
if(cx>zq) cout<<"\n猜大了\n";
else if(cx<zq) cout<<"\n猜小了\n";
else if(cx==zq) {
printf("\n恭喜你猜对了!, 答案就是%d\n", zq);
a=true;
break;
}
if(i==cs&&a==false) cout<<"\n次数耗尽!\n";
}
getchar();
printf("还要继续吗? 不要请输入0: ");
cin>>flag;
system("cls");
}
system("cls");
cout<<"欢迎您下次使用";
cout<<"\n创作者:C++程序师(优快云博客名)";
return 0;
}
479

被折叠的 条评论
为什么被折叠?



