各位大佬,请帮忙看一下,为什么输出总为0

G-F-N 2020-03-04 11:44:31
#include <stdio.h>

int main()
{
int num,num0=0,num1=0,num2=0,num3=0,num4=0,num5=0;
char operation=0;
printf("Please think of a number less than 50 in your mind!\n");
printf("Then I will give you a set of cards, please note that if your numbers are in those cards\n");
printf("I will figure out the number a piece of cake after that\n");
printf("okay, let us try!\n");
printf("Enter 'q' to end input\n");
printf("You can input like that 0#卡片 or 2#卡片 :\n");

while(1)
{
scanf("%c",&operation);
if(operation == '0#卡片')
{
num0=1;
continue;
}
if(operation == '1#卡片')
{
num1=2;
}
if(operation == '2#卡片')
{
num2=4;
}
if(operation == '3#卡片')
{
num3=8;
}
if(operation == '4#卡片')
{
num4=16;
}
if(operation == '5#卡片')
{
num5=32;
}
if(operation == 'q')
{
num = num0+num1+num2+num3+num4+num5;
break;
}

}
printf("%d",num);
return 0;
}
这个输出为什么总是0啊?
...全文
136 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2020-03-05
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

int main()
{
int num,num0=0,num1=0,num2=0,num3=0,num4=0,num5=0;
char operation[32];

printf("Please think of a number less than 50 in your mind!\n");
printf("Then I will give you a set of cards, please note that if your numbers are in those cards\n");
printf("I will figure out the number a piece of cake after that\n");
printf("okay, let us try!\n");
printf("Enter 'q' to end input\n");
printf("You can input like that 0#卡片 or 2#卡片 :\n");

while(1)
{
scanf("%s", operation);
if(strcmp(operation, "0#卡片") == 0)
{
num0=1;
continue;
}
if(strcmp(operation, "1#卡片") == 0)
{
num1=2;
}
if(strcmp(operation, "2#卡片") == 0)
{
num2=4;
}
if(strcmp(operation, "3#卡片") == 0)
{
num3=8;
}
if(strcmp(operation, "4#卡片") == 0)
{
num4=16;
}
if(strcmp(operation, "5#卡片") == 0)
{
num5=32;
}
if(strcmp(operation, "q") == 0)
{
num = num0+num1+num2+num3+num4+num5;
break;
}

}
printf("%d",num);
return 0;
}

供参考~

字符串和字符的含义建议楼主重新温习一下~
qybao 2020-03-05
  • 打赏
  • 举报
回复
你这个输入的是字符串啊
所以改成%s输入,即

char operation[16] = {""}; //这里改成字符数组

while(1)
{
scanf("%s",&operation); //这里改成%s
getchars(); //去掉输入缓存的回车
if(strcmp(operation, "0#卡片")==0) //这里用字符串比较函数strcmp,剩下的自己修改吧

70,023

社区成员

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

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