来看看我的C语句哪里错了?
来源:百度知道 编辑:UC知道 时间:2024/05/02 21:11:48
main()
{
char a[2048];
char buf[2048]= "1234";
printf("欢迎使用英汉词典\n");
this: printf("请输入一个单词或命令:");
scanf("%s",a);
switch(a[2048]);
{
case a[2048]=="apple" : printf("苹果\n"); goto this;
case a[2048]=="so" : printf("因此\n"); goto this;
case a[2048]=='"ESC" : printf("谢谢使用");break;
default : printf("搜索不到您要找的单词,或者您输入的命令错误请重新输入"); goto this;
}
}
{
char a[2048];
char buf[2048]= "1234";
printf("欢迎使用英汉词典\n");
this: printf("请输入一个单词或命令:");
scanf("%s",a);
switch(a[2048]);
{
case a[2048]=="apple" : printf("苹果\n"); goto this;
case a[2048]=="so" : printf("因此\n"); goto this;
case a[2048]=='"ESC" : printf("谢谢使用");break;
default : printf("搜索不到您要找的单词,或者您输入的命令错误请重新输入"); goto this;
}
}
case支持的数据类型有限,就整形和Char型还有枚举,所以像楼主这种字符串的比较还是老老实实用if和else来实现逻辑,用strcmp来比较吧。
改造后大约是这样的:
前略
if(strcmp(a, "apple")) {
printf("苹果\n"); goto this;
}else if(strcmp(a, "so")) {
printf("因此\n"); goto this;
}else if(strcmp(a, "ESC")) {
printf("谢谢使用");
}else {
printf("搜索不到您要找的单词,或者您输入的命令错误请重新输入"); goto this;
}
case 这里出现了问 a[2048]里的每个元素要分别和apple的每个单词做对比 如a[0]==a a[1]==p a[2]==p a[3]==l a[4]==e 这样就可以了
a[]是个字符串数组,每个元素只能记录一个字符而且要以单引号赋值
case a[0]=='a'&&a[1]=='p'&&a[2]=='p'&&a[3]=='l'&&a[4]=='e'