c++小游戏来啦!写啦了15分钟,代码放这儿啦!有注释,可以细细品味(´ω`)
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int n,m,o,u,t,cnt=1;
int random(int x, int y) {
return rand()%(y-x+1)+n;
}//计算随机值
int an(int a,int b,int c){
return (a-b)/c;
}//计算最少要猜多少次
int main(){
cout<<"请输入最小值和最大值!"<<endl;
cin>>n>>m;
o=random(n,m);//使用random计算随机值
u=an(m,n,o);//使用an计算最少要猜多少次
cout<<"你要最少要猜"<<u<<"次'-'"<<endl;
cout<<"好了,开始猜吧!"<<endl;
f:
cin>>t;
if(t==o&&cnt>=u){
system("cls");//windows.h中的函数,清空页面
cout<<"哦!被你猜到了^-^,我就说你要猜"<<u<<"次以上吧^.^";
cout<<"你猜了"<<cnt<<"次";
}else if(t==o&&cnt<u){
system("cls");//windows.h中的函数,清空页面
cout<<"什么?你猜"<<cnt<<"次就猜中了'-',我不甘心`.`!";
}else if(t>o){
cout<<"大了哦!"<<endl;
cnt++;
goto f;//回到f,继续输入
}else if(t<o){
cout<<"小了哦!"<<endl;
cnt++;
goto f;//回到f,继续输入
}else if(t==0){
cout<<"不能输出0!"<<endl;
goto f; //回到f,继续输入
}
return 0;
}