《带你学C带你飞》动动手作业题S1E31执行错误为什么
本帖最后由 奥普瓯江 于 2017-9-22 11:08 编辑题目是
为什么我写得程序执行不了,第一个问题是,洗牌时执行选项失败没有反馈,还有就是洗牌“ad”指针数组第1个是空字符我也不知道问什么,还麻烦大神给予解答谢谢
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 1024
#define NAX 14
char *sp ={\
"方A","方2","方3","方4","方5","方6","方7","方8","方9","方10",\
"梅A","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10",\
"红A","红2","红3","红4","红5","红6","红7","红8","红9","红10",\
"黑A","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10",\
"方J","方Q","方K","梅J","梅Q","梅K","红J","红Q","红K","黑J",\
"黑Q","黑K","大王","小王"\
};
char *ssp; //接收字符串
char *ad, *bd, *cd;//每个人最可以拿多少张牌
char fapai();
char xipai();
char dayin();
char fapai() //发牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp = sp;
}
}
char xipai() //洗牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp = sp;
}
}
char dayin() //打印函数程序
{
int d = 0, k = 0, g = 0;
for(int i = 0; i < 42; i++)
{
if (i >= 0 && i < 14)
{
d++;
ad= ssp;
}
else if(i >= 14 && i < 28)
{
k++;
bd= ssp;
}
else if(i >= 28 && i < 42)
{
g++;
cd= ssp;
}
}
}
int main()
{
char *a, *b, *c;
printf("请输入1号玩家的名字:");
scanf("%s",&a);
printf("请输入2号玩家的名字:");
scanf("%s",&b);
printf("请输入3号玩家的名字:");
scanf("%s",&c);
printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n");
fapai();
dayin();
printf("%s手上的牌是: ", a);
for (int i = 0; i < NAX; i++)
{
printf("%s", ad);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (int i = 0; i < NAX; i++)
{
printf("%s", bd);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (int i = 0; i < NAX; i++)
{
printf("%s", cd);
}
putchar('\n');
printf("重新洗牌(Y/N)? ");
char ch;
while(1)
{
scanf("%c", &ch);
getchar();
if ( ch == 'Y' )
{
xipai();
dayin();
printf("%s手上的牌是: ", a);
for (int i = 0; i < NAX; i++)
{
printf("%s", ad);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (int i = 0; i < NAX; i++)
{
printf("%s", bd);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (int i = 0; i < NAX; i++)
{
printf("%s", cd);
}
putchar('\n');
printf("\n重新洗牌(Y/N)? ");
}
else if ( ch == 'N' )
{
break;
}
else
{
printf("\n您输入的字符有误\n");
printf("\n重新洗牌(Y/N)? ");
}
}
return 0;
} 本帖最后由 ba21 于 2017-9-22 13:15 编辑
1,d++ i++ g++;请放到后面;
2, Y/N 再次接收时
while(getchar()!='\n') //#清空键盘缓冲区数据
continue;
3,fapai() ; xipai(); 就合成一个洗牌就好了。
4,随机数有重复(我这里也没有解决)
参数代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 1024
#define NAX 14
char *sp ={\
"方A","方2","方3","方4","方5","方6","方7","方8","方9","方10",\
"梅A","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10",\
"红A","红2","红3","红4","红5","红6","红7","红8","红9","红10",\
"黑A","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10",\
"方J","方Q","方K","梅J","梅Q","梅K","红J","红Q","红K","黑J",\
"黑Q","黑K","大王","小王"\
};
char *ssp; //接收字符串
char *ad, *bd, *cd;//每个人最可以拿多少张牌
void xipai();
void dayin();
void xipai() //洗牌函数
{
int i;
srand((unsigned)time(NULL)); //初始化rand随机变量的值
for (i= 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在0-53内
ssp = sp;
}
}
void dayin() //打印函数程序
{
int d = 0, k = 0, g = 0;
int i;
for(i = 0; i < 42; i++)
{
if (i >= 0 && i < 14)
{
ad= ssp;
d++;
}
else if(i >= 14 && i < 28)
{
bd= ssp;
k++;
}
else if(i >= 28 && i < 42)
{
cd= ssp;
g++;
}
}
}
int main(void)
{
char *a, *b, *c;
int i;
char ch;
printf("请输入1号玩家的名字:");
scanf("%s",&a);
printf("请输入2号玩家的名字:");
scanf("%s",&b);
printf("请输入3号玩家的名字:");
scanf("%s",&c);
printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n");
xipai();
dayin();
printf("%s手上的牌是: ", a);
for (i = 0; i < NAX; i++)
{
printf("%s ", ad);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (i = 0; i < NAX; i++)
{
printf("%s ", bd);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (i = 0; i < NAX; i++)
{
printf("%s ", cd);
}
putchar('\n');
printf("重新洗牌(Y/N)? ");
while(getchar()!='\n') //#清空键盘缓冲区数据
continue;
while(1)
{
scanf("%c", &ch);
while(getchar()!='\n') //#清空键盘缓冲区数据
continue;
if ( ch == 'Y' )
{
xipai();
dayin();
printf("%s手上的牌是: ", a);
for (i = 0; i < NAX; i++)
{
printf("%s ", ad);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (i = 0; i < NAX; i++)
{
printf("%s ", bd);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (i = 0; i < NAX; i++)
{
printf("%s ", cd);
}
putchar('\n');
printf("\n重新洗牌(Y/N)? ");
}
else if ( ch == 'N' )
{
break;
}
else
{
printf("\n您输入的字符有误\n");
printf("\n重新洗牌(Y/N)? ");
}
}
return 0;
} 下面是我帮你精简的程序代码:(红色为错误修改的地方)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 1024
#define NAX 14
char *sp ={\
"方A","方2","方3","方4","方5","方6","方7","方8","方9","方10",\
"梅A","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10",\
"红A","红2","红3","红4","红5","红6","红7","红8","红9","红10",\
"黑A","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10",\
"方J","方Q","方K","梅J","梅Q","梅K","红J","红Q","红K","黑J",\
"黑Q","黑K","大王","小王"\
};
char *ssp; //接收字符串
char *ad, *bd, *cd;//每个人最可以拿多少张牌
char a, b, c;/*定义一维数组就可以了,否则下面使用的scanf的时候编译会有问题*/
void fapai();
void xipai();
void dayin();
void fapai() //发牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp = sp;
}
}
void xipai() //洗牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp = sp;
}
}
void dayin() //打印函数程序
{
int d = 0, k = 0, g = 0;
for(int i = 0; i < 42; i++)
{
if (i >= 0 && i < 14)
{
ad= ssp;
d++;
}
else if(i >= 14 && i < 28)
{
bd= ssp;
k++;
}
else if(i >= 28 && i < 42)
{
cd= ssp;
g++;
}
}
printf("%s手上的牌是: ", a);
for (int i = 0; i < NAX; i++)
{
printf("%s ", ad);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (int i = 0; i < NAX; i++)
{
printf("%s ", bd);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (int i = 0; i < NAX; i++)
{
printf("%s ", cd);
}
}
int main()
{
printf("请输入1号玩家的名字:");
scanf("%s",a);
printf("请输入2号玩家的名字:");
scanf("%s",b);
printf("请输入3号玩家的名字:");
scanf("%s",c);
printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n");
fapai();
dayin();
char ch;
while(getchar()!='\n')
continue;
while(1)
{
putchar('\n');
printf("\n重新洗牌(Y/N)? ");
scanf("%c", &ch);
while(getchar()!='\n')
continue;
/*大小写都需要支持*/
if ( ch == 'Y' || ch=='y' )
{
xipai();
dayin();
}
else if ( ch == 'N' || ch=='n' )
{
printf("\n退出游戏\n ");
break;
}
else
{
printf("\n您输入的字符有误\n");
}
}
return 0;
} #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 1024
#define NAX 14
char *sp ={\
"方A","方2","方3","方4","方5","方6","方7","方8","方9","方10",\
"梅A","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10",\
"红A","红2","红3","红4","红5","红6","红7","红8","红9","红10",\
"黑A","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10",\
"方J","方Q","方K","梅J","梅Q","梅K","红J","红Q","红K","黑J",\
"黑Q","黑K","大王","小王"\
};
char *ssp; //接收字符串
char *ad, *bd, *cd;//每个人最可以拿多少张牌
char a, b, c;
void fapai();
void xipai();
void dayin();
void fapai() //发牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp = sp;
}
}
void xipai() //洗牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp = sp;
}
}
void dayin() //打印函数程序
{
int d = 0, k = 0, g = 0;
for(int i = 0; i < 42; i++)
{
if (i >= 0 && i < 14)
{
ad= ssp;
d++;
}
else if(i >= 14 && i < 28)
{
bd= ssp;
k++;
}
else if(i >= 28 && i < 42)
{
cd= ssp;
g++;
}
}
printf("%s手上的牌是: ", a);
for (int i = 0; i < NAX; i++)
{
printf("%s ", ad);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (int i = 0; i < NAX; i++)
{
printf("%s ", bd);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (int i = 0; i < NAX; i++)
{
printf("%s ", cd);
}
}
int main()
{
printf("请输入1号玩家的名字:");
scanf("%s",a);
printf("请输入2号玩家的名字:");
scanf("%s",b);
printf("请输入3号玩家的名字:");
scanf("%s",c);
printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n");
fapai();
dayin();
char ch;
while(getchar()!='\n')
continue;
while(1)
{
putchar('\n');
printf("\n重新洗牌(Y/N)? ");
scanf("%c", &ch);
while(getchar()!='\n')
continue;
if ( ch == 'Y' || ch=='y' )
{
xipai();
dayin();
}
else if ( ch == 'N' || ch=='n' )
{
printf("\n退出游戏\n ");
break;
}
else
{
printf("\n您输入的字符有误\n");
}
}
return 0;
} ba21 发表于 2017-9-22 13:12
1,d++ i++ g++;请放到后面;
2, Y/N 再次接收时
while(getchar()!='\n') //#清空键盘缓冲区数据
谢谢二楼帮忙纠正了我的错误,学习了麻烦 weizhongyang 发表于 2017-9-22 14:10
下面是我帮你精简的程序代码:(红色为错误修改的地方)
#include
也谢谢三楼帮忙优化代码麻烦了 ba21 发表于 2017-9-22 13:12
1,d++ i++ g++;请放到后面;
2, Y/N 再次接收时
while(getchar()!='\n') //#清空键盘缓冲区数据
还有一个额外的提问就是为什么要清空键盘缓冲区数据,我在往上找了一下只知道使用的方法了,但是没有理解,还麻烦能否给推荐一下或者给解释一下谢谢 奥普瓯江 发表于 2017-9-22 15:28
还有一个额外的提问就是为什么要清空键盘缓冲区数据,我在往上找了一下只知道使用的方法了,但是没有理解 ...
一行内容,就不算其它的,最后也还有一个\n ba21 发表于 2017-9-22 15:29
一行内容,就不算其它的,最后也还有一个\n
这个明白,但是为什么getchar()就能消除\n 呢
奥普瓯江 发表于 2017-9-22 15:31
这个明白,但是为什么getchar()就能消除\n 呢
getchar() 获取字符啊。能不消除 ba21 发表于 2017-9-22 15:33
getchar() 获取字符啊。能不消除
明白了谢谢,他是获取了第二个字符\n
页:
[1]