#include <bits/stdc++.h>
#define ll long long //将long long替换为自定义名称,节省打字时间
using namespace std;
bool QJFWPD(ll num,ll a,ll b){ //定义函数,作用是判断一个数是否在另外两个数之间,如果是则返回true,否则返回false
if(a<b && num>=a && num<=b) return true;
else if(b<a && num>=b && num<=a) return true;
else return false;
}
int main(){
srand(time(0)); //时间初始化
ll a,b,guess,sjs; //定义所需变量
for(int i=1;i==1;i=1){ //无限循环
cout<<"Please enter the interval range:"<<endl; //输入提示
cin>>a>>b; //输入范围
if(a>b){ //难度不能过小,如果过小则结束程序
if(a-b<=10) return 0;
}else if(a<b){
if(b-a<=10) return 0;
}else if(a==b) return 0;
if(a<b) sjs=rand()%b+a; //判断输入的数那个更大,并生成对应范围的两个随机数
else if(a>b)sjs=rand()%a+b;
cout<<"Please enter your guess:"<<endl; //输入提示
cin>>guess; //输入猜的数
for(int j=1;j==1;j=1){ //无限循环,如果猜对了则跳出循环继续下一次,否则告诉你大了或者小了,如果输入的数不在范围内则重新输入猜的数
if(QJFWPD(guess,a,b)){
if(guess==sjs){
cout<<"You win!"<<endl;
break;
}else if(guess<sjs){
cout<<"Your guess was small."<<endl;
cin>>guess;
}else if(guess>sjs){
cout<<"Your guess was big."<<endl;
cin>>guess;
}
}else{
cout<<"The number entered is not in the range,please re-enter it:"<<endl;
cin>>guess;
}
}
}
return 0;
}
c++创意编程-猜随机数
最新推荐文章于 2024-06-16 16:51:09 发布