我写了一段代码,试图判断用户输入的三个数字中哪个数字最大。但是,我无法理解为什么我的代码在输入 3, 1, 2
时中断,而在输入 55, 54, 56
时有效。
我的代码:
main()
{
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest",a);
if(b>a && b>c)
printf("%d is greatest",b);
else printf("%d is greatest",c);
getch();
}
我在做什么导致了这个错误,我该怎么做才能修复它?
最佳答案
你肯定漏掉了“else if”。
main()
{
int a,b,c;
printf("enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest",a);
else if(b>a && b>c)
printf("%d is greatest",b);
else
printf("%d is greatest",c);
}
关于c - 最大的 3 个数字代码让我困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10203425/