c++猜数游戏代码

​
#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段代码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值