我是c语言初学者,有些问题不懂.#includemain(){\x05int a,b;\x05\x05scanf("%d,%d",&a,&b);\x05\x05if(a>b) printf("%d is bigger than %d\n",a,b);\x05\x05if(a==b) printf("%d is equal to %d\n",a,b);\x05\x05else printf("%d is bigger than %d\n",b,a

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/12 00:48:15

我是c语言初学者,有些问题不懂.#includemain(){\x05int a,b;\x05\x05scanf("%d,%d",&a,&b);\x05\x05if(a>b) printf("%d is bigger than %d\n",a,b);\x05\x05if(a==b) printf("%d is equal to %d\n",a,b);\x05\x05else printf("%d is bigger than %d\n",b,a
我是c语言初学者,有些问题不懂.
#include
main()
{
\x05int a,b;
\x05\x05scanf("%d,%d",&a,&b);
\x05\x05if(a>b) printf("%d is bigger than %d\n",a,b);
\x05\x05if(a==b) printf("%d is equal to %d\n",a,b);
\x05\x05else printf("%d is bigger than %d\n",b,a);
}
这是我编的比较大小的程序,运行的时候,输入10,5会出现
10 is bigger than 5
5 is bigger than 10
为什么会出现两个?后面的还是错的!
我是不是哪里编错了?

我是c语言初学者,有些问题不懂.#includemain(){\x05int a,b;\x05\x05scanf("%d,%d",&a,&b);\x05\x05if(a>b) printf("%d is bigger than %d\n",a,b);\x05\x05if(a==b) printf("%d is equal to %d\n",a,b);\x05\x05else printf("%d is bigger than %d\n",b,a
同意mars_room_10的回答.
程序是从上往下运行的,你的两个if是并列关系.
输入5和10之后,满足第一个if(a>b),所以会出现“10 is bigger than 5”
判断完之后它就会进入第二个if,很明显a==b是不成立的,所以就会执行else里的代码,出现“5 is bigger than 10”.
在第二个if 前面加个 else,把两个if变成嵌套关系,只有当不满足a>b的时候,才会去判断a==b满不满足.