关于swich语句求助大神们

newcomer_mz 2010-03-23 01:26:06
大神们帮帮忙啊 、、、%>_<%
程序老是 出错或者得不到想要的,程序主体没错,我其实就是想用switch语句编辑一个菜单方便我输入数字1 2 后就可以得到
我想要的。其实就是使用菜单选择操作,提示用户进行相应操作, 但是我的swich语句 用的不对劲额。。。程序主体的求解部分没有错误(这个单独试过)关键是SWICH语句的使用
各位教教我哈~十分感谢。。最好教我怎么用,简略点帮我改改~O(∩_∩)O谢谢
#include<stdio.h>
int score[4][4],choose;
void main()
{ printf("welcome to my design !my c is who run the least fast \n");
printf("if you want to go the program without thinking ,please put into 1\n");
printf("if you wan to go over slightly, please put into 2\n ");
switch(choose)
{case1:{int i,j,k,who;
score[1][1]=7;
score[2][1]=8;
score[3][1]=9;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;i!=j&&k<=3;k++)
if(k!=i&&k!=j&&15-i-score[1][1]!=15-j-score[2][1]
&&15-i-score[1][1]!=15-k-score[3][1]
&&15-j-score[2][1]!=15-k-score[3][1]
&&15-i-score[1][1]!=i+1
&&15-j-score[2][1]!=j+1
&&15-k-score[2][1]!=k+1)
{
score[1][2]=i;score[1][3]=15-i-7;
score[2][2]=j;score[2][3]=15-j-8;
score[3][2]=k;score[3][3]=15-k-9;
}
for(who=0,i=1;i<=3;i++,printf("\n"))
for(j=1;j<=3;j++)
{
printf("%d",score[i][j]);
if(score[i][j]==1)who=i;
}
if(who==1)

printf("The last one arrived to end is a child from family Zhang.\n");
else if(who==2)
printf("The last one arrived to end is a child from family Wang.\n");
else printf("The last one arrived to end is a child from family Li.\n");}
caes2:printf("the program is over, thank you for your fitting\n");
default:break;
}
}
...全文
182 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2010-03-23
  • 打赏
  • 举报
回复
单步调试和设断点调试是程序员必须掌握的技能之一。
newcomer_mz 2010-03-23
  • 打赏
  • 举报
回复
谢谢各位大神额。。代码好了 谢谢!O(∩_∩)O~
up522641996 2010-03-23
  • 打赏
  • 举报
回复
楼上正解
huanmie_09 2010-03-23
  • 打赏
  • 举报
回复
没有输入choose变量,因为定义的是全局变量,因此会初始化为0.
如果要接受输入,可以在switch语句前面添加scanf("%d", choose);
huanmie_09 2010-03-23
  • 打赏
  • 举报
回复
没有看你逻辑的处理,switch语句case分支后,如果你想处理完后就跳出switch,就在每个case后面添加一个break;

#include<stdio.h>

int score[4][4],choose;

void main()
{
printf("welcome to my design !my c is who run the least fast \n");
printf("if you want to go the program without thinking ,please put into 1\n");
printf("if you wan to go over slightly, please put into 2\n ");
switch(choose) {
case1: {
int i,j,k,who;
score[1][1]=7;
score[2][1]=8;
score[3][1]=9;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;i!=j&&k<=3;k++)
if(k!=i&&k!=j&&15-i-score[1][1]!=15-j-score[2][1]
&&15-i-score[1][1]!=15-k-score[3][1]
&&15-j-score[2][1]!=15-k-score[3][1]
&&15-i-score[1][1]!=i+1
&&15-j-score[2][1]!=j+1
&&15-k-score[2][1]!=k+1)
{
score[1][2]=i;
score[1][3]=15-i-7;
score[2][2]=j;
score[2][3]=15-j-8;
score[3][2]=k;
score[3][3]=15-k-9;
}
for(who=0,i=1;i<=3;i++,printf("\n"))
for(j=1;j<=3;j++) {
printf("%d",score[i][j]);
if(score[i][j]==1)
who=i;
}
if(who==1)
printf("The last one arrived to end is a child from family Zhang.\n");
else if(who==2)
printf("The last one arrived to end is a child from family Wang.\n");
else
printf("The last one arrived to end is a child from family Li.\n");
break; /*添加break*/
}
caes2:
printf("the program is over, thank you for your fitting\n");
break; /*添加break*/
default:break;
}
}

xiaoboalex 2010-03-23
  • 打赏
  • 举报
回复
在switch(choose)之前添加scanf("%d", &choose);
yuzl32 2010-03-23
  • 打赏
  • 举报
回复
scanf("%d",&choose);//1.
switch(choose)
{
case1:{int i,j,k,who;
score[1][1]=7;
...
stardust20 2010-03-23
  • 打赏
  • 举报
回复
#include<stdio.h> 
int score[4][4],choose;
void main()
{
printf("welcome to my design !my c is who run the least fast \n");
printf("if you want to go the program without thinking ,please put into 1\n");
printf("if you wan to go over slightly, please put into 2\n ");
scanf("%d",&choose);//你没读入选择啊
switch(choose)
{
case 1://case和1要有个空格
{
int i,j,k,who;
score[1][1]=7;
score[2][1]=8;
score[3][1]=9;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;i!=j&&k<=3;k++)
if(k!=i&&k!=j&&15-i-score[1][1]!=15-j-score[2][1]
&&15-i-score[1][1]!=15-k-score[3][1]
&&15-j-score[2][1]!=15-k-score[3][1]
&&15-i-score[1][1]!=i+1
&&15-j-score[2][1]!=j+1
&&15-k-score[2][1]!=k+1)
{
score[1][2]=i;score[1][3]=15-i-7;
score[2][2]=j;score[2][3]=15-j-8;
score[3][2]=k;score[3][3]=15-k-9;
}
for(who=0,i=1;i<=3;i++,printf("\n"))
for(j=1;j<=3;j++)
{
printf("%d",score[i][j]);
if(score[i][j]==1)who=i;
}
if(who==1)

printf("The last one arrived to end is a child from family Zhang.\n");
else if(who==2)
printf("The last one arrived to end is a child from family Wang.\n");
else printf("The last one arrived to end is a child from family Li.\n");
break;
}
case 2:printf("the program is over, thank you for your fitting\n");
default:break;
}
}
xiaoboalex 2010-03-23
  • 打赏
  • 举报
回复
case1 --> case 1
caes2 --> case 2
james_hw 2010-03-23
  • 打赏
  • 举报
回复
教你一个方法,case呢,对应一个函数,把你那么长的代码都放到函数里实现,代码好看又容易检查

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧