#include<bits/stdc++.h> //万能头文件
#include<windows.h>
using namespace std;
int main()
{
int suij1,suij2,f1,f2,n1=1;
cout<<"\t\t\t\t\t这是一个猜数游戏,祝你好运\n";
cout<<"\t\t\t 请问是自己输入随机数取值范围还是从0~99之间取随机数?\n------------------------------------------------------------------------------------------------------------------------\n\t\t\tA.自己输入\t\tB.从0~99取随机数\t\t输入ESC退出\n"; //作者技术有限,只能用string输入
string x1;
a:
cin>>x1;
if(x1=="A")
{
Sleep(500);
system("cls");
cout<<"请输入随机数取值范围:\t(先输入大数,再输入小数)\n";
cin>>f1>>f2;
srand(time(0));
suij1=rand()%(f1-f2)+f1;
cout<<"本次随机数取值范围在"<<f1<<"~"<<f2<<"中区随机数\n请输入你猜的数:\n";
int c1;
do{
cin>>c1;
if(c1<suij1)
{
cout<<"猜小了\n";
n1++;
}
else if(c1>suij1)
{
cout<<"猜大了\n";
n1++;
}
else if(c1==suij1)
{
cout<<"你猜对了,共猜了"<<n1<<"次";
return 0;
}
}while(c1!=suij1);
}
else if(x1=="B")
{
Sleep(500);
system("cls");
srand(time(0));
suij2=rand()%100;
cout<<"本次随机数取值范围在0~99之间取随机数\n请输入你猜的数:\n";
int c1;
do{
cin>>c1;
if(c1<suij2)
{
cout<<"猜小了\n";
n1++;
}
else if(c1>suij2)
{
cout<<"猜大了\n";
n1++;
}
else if(c1==suij2)
{
cout<<"你猜对了,共猜了"<<n1<<"次";
return 0;
}
}while(c1!=suij2);
}
else if(x1=="ESC")
{
Sleep(500);
system("cls");
system("pause");
return 0;
}
else if(x1!="A"||x1!="B"||x1!="ESC")
{
cout<<"输入错误,请重新选择\n";
goto a;
}
}
实现函数:
int a;
srand(time(0)); a=rand(); //如果没有限制,那随机数范围就在a的存值范围
srand(time(0)); a=rand()%100+1; //在0~100之间取随机数
system("cls"); //清屏
system("pause"); //等待
goto a; //跳转制a段代码