📄 guessthenumberofgames.txt
字号:
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
void judge1(char *ch,int sort1); //judge1只是使输入的数是一个字节,judge2使输入的数是0--9中的一个
void judge2(char *ch,int sort2);
void play();
void judge1(char *ch,int sort1)
{
for(;;)
{
sort1++;
if(strlen(ch)==1)
break;
else
{
if(sort1>3)
{
cout<<"您的输入错误次数已到限制,要继续请重新开始!\n";
exit(0);
}
cout<<"您的输入有误,请重新输入: ";
cin>>ch;
judge1(ch,sort1); //循环调用函数
}
}
}
void judge2(char *ch,int sort2)
{
for(;;)
{
sort2++;
if(*ch<'9'&&*ch>'0')
break;
else
{
if(sort2>3)
{
cout<<"你的输入错误次数已到限制,要继续请重新开始!\n";
exit(0);
}
cout<<"你的输入有误,请重新输入: ";
cin>>ch;
judge1(ch,sort2); //使输入的数字是一个字节
judge2(ch,sort2); //循环调用函数
}
}
}
void con()
{
char ch;
int stat5=1;
cout<<"还要继续吗?(Y/N?) ";
cin>>ch;
if(ch=='Y'||ch=='y')
play();
else if(ch=='N'||ch=='n')
{cout<<"感谢您的参与,敬请再次游戏.";exit(0);}
else {cout<<"error input!\n";exit(0);}
stat5++;
if(stat5>5)
{cout<<"您的继续次数已到限制,要继续请重新开始,谢谢!\n";exit(0);}
}
void play2(char n[],int num)
{
char num2=num+48;
int i=3,sort4=1;
while(n[0]!=num2&&i>1)
{
if(n[0]>num2)
cout<<"您输入的数比随机数大,你还有"<<i-1<<"次机会: ";
else if(n[0]<num2)
cout<<"您输入的数比随机数小,你还有"<<i-1<<"次机会: ";
cin>>n;
judge1(n,sort4);
judge2(n,sort4);
}
if(n[0]==num2)
cout<<"恭喜你,猜对了.随机数正是 "<<n<<endl;
else if(i==1)
cout<<"您的错误次数已到三次,这次的随机数是 "<<num<<endl;
}
void play()
{
char n[5];
int stat=1,num;
srand((unsigned)time(NULL));
for(;;)
{
num=rand()%10;
cout<<"请输入0--9中的一个数字: ";
cin>>n;
judge1(n,stat);
judge2(n,stat);
play2(n,num);
con();
}
}
void rule()
{
char judge;
cout<<"\n. 游戏的规则:输入0--9中的一个数字,然后根据提示输入数字,如果你输入的数字和随机数相等,那么你赢的了比赛,你可以选择继续或者退出。\n";
cout<<"要继续吗(y/n?)";
cin>>judge;
if(judge=='y'||judge=='Y')
play();
else if(judge=='n'||judge=='N')
exit(0);
else cout<<"error input!\n";
}
void main()
{
int choice;
cout<<" 欢迎进入猜数小游戏,直接进入请输入0,查看规则请输入1 ";
cin>>choice;
if(choice==0)
play();
else if(choice==1)
rule();
else
cout<<"您的输入有误,请重新开始!\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -