碎碎念
听翁老师的课对if else 语句不太懂,因此做一个练习。
对嵌套式if else 语句的练习
一开始的错误版:
#include <stdio.h>
int main()
{
int a,b,c,max;
scanf("%d,%d,%d",&a,&b,&c);
max=0;
if (a>b)
{if (a>c)
max=a;
else
max=c;}
else {if (b>c)
max=b;
else
max=c;}
return 0;
}
然后运行,系统没有报错,但结果一直不对,我以为是if语句没写好,把它拆成两部分进行测试,结果if语句没有问题,问题在上面:
scanf("%d,%d,%d")——实际上中间分隔应该用空格,不能用逗号隔开。
最后加入提示语句,完成这个小程序。
#include <stdio.h>
int main()
{
int a,b,c,max;
printf("please input three numbers.\nI will return the max.\n");
scanf("%d %d %d",&a,&b,&c);
max=0;
printf("You print %d,%d,%d.",a,b,c);
if (a>b){
if (a>c){
max=a;}
else
{max=c;}}
else {if (b>c)
{max=b;}
else
{max=c;}}
printf("The max is %d\n.",max);
return 0;
}
总结
scanf()输入时不要用逗号分隔,打空格不会出错。