JS,跟JX是在主函数中定义的,由于作用域的限制,除了用参数传递的方法外,不能在其他的函数中调用;int sr1,sr2;也是这个问题,它是在非主函数中定义的,在主函数中无法引用,除了用参数传递的方法外,不能在其他的函数中调用。
int pd1;要判断pd1是否等于'Y',应该定义pd1为char型数据
另外,想在一个函数中定义的变量在其他函数中引用,用关键字extern来标明
如:
fun1()
{
int a=0;
}
fun2()
{
extern int a;
}
这样fun2中的a也等于0
具体用法可以到网上搜,有很多
这是修改后的程序
#include
#include
#include
using namespace std;
int main()
{
int J_sr(int &s1,int &s2);
int J_sj();
int J_pd(int &s1,int &s2);
int sr1,sr2;
J_pd(sr1,sr2);
return 0;
}
int J_sr(int &sr1,int &sr2) //输入输出函数
{
cout<<"请选择:"<
cin>>sr1;
switch(sr1)
{
case 0:
cout<<"您选择的是石头"<
cout<<"您选择的是剪刀"<
cout<<"您选择的是布"<
cout<<"输入有误请重新输入:";
goto label;
}
return sr1;
}
int J_sj() //随机函数
{
int sj1;
srand((unsigned)time(NULL));
sj1=rand()%3;
switch(sj1)
{
case 0:cout<<"对方选择的是石头"<
}
return sj1;
}
int J_pd(int &sr1,int &sr2)//判断函数
{
JX:
int a=J_sr(sr1,sr2);
int b=J_sj();
char pd1;
if (a-b==0)
{
cout<<"平局!"<
if (pd1=='Y'||pd1=='y')
J_pd(sr1,sr2);
else goto JS;
}
if (a==0&&b==1)
{
cout<<"你赢了!"<
if (pd1=='Y'||pd1=='y')
goto JX;
else goto JS;
}
if (a==0&&b==2)
{
cout<<"你输了!"<
if (pd1=='Y'||pd1=='y')
goto JX;
else goto JS;
}
if (a==1&&b==0)
{
cout<<"你输了!"<
if (pd1=='Y'||pd1=='y')
goto JX;
else goto JS;
}
if(a==1&&b==2)
{
cout<<"你赢了!"<
if (pd1=='Y'||pd1=='y')
goto JX;
else goto JS;
}
if (a==2&&b==0)
{
cout<<"你赢了!"<
if(pd1=='Y'||pd1=='y')
goto JX;
else goto JS;
}
if (a==2&&b==1)
{
cout<<"你输了!"<
if (pd1=='Y'||pd1=='y')
goto JX;
else
goto JS;
}
JS:
cout<<"结束"<
}
这是因为你的sr2,sj1这两个变量的使用在定义之前了,你把MAIN函数放到其他函数的下面。