c++小游戏:猜数游戏

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值