-2 c if-statement
/* to find the age of individuals according to youngest to oldest */
#include <stdio.h>
int main(void)
{
int age1, age2, age3, youngest, middle, oldest;
{
printf ("Enter the age of the first individual: ");
scanf ("%d", &age1);
printf ("Enter the age of the second individual: ");
scanf ("%d", &age2);
printf ("Enter the age of the third individual: ");
scanf ("%d", &age3);
}
if (age1==age2==age3);
{
printf("All individuals have the same age of %d", &age1);
}
else
{
youngest = age1;
if (age1 > age2)
youngest = age2;
if (age2 > age3)
youngest = age3;
middle = age1;
if (age1 > age2)
middle = age2;
if (age2 < age3)
middle = age2;
oldest = age1;
if (age1 < age2)
oldest = age2;
if (age2 < age3)
oldest = age3;
printf("%d is the youngest.\n", youngest);
printf("%d is the middle.\n", middle);
printf("%d is the oldest.\n", oldest);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我一直在第21行得到错误,该错误表明我有一个'else'和之前的'if'.这里的任何专家都可以告诉我哪里出错了?如果我要删除'else',显示也有点奇怪.
在你的代码中
if (age1==age2==age3);
Run Code Online (Sandbox Code Playgroud)
是可怕的破碎.
两个要点,
像的表达age1==age2==age3是要么
0 == age3 , 什么时候 age1 != age21 == age3 , 什么时候 age1 == age2没有你想要的.
将;在结束if发言使得下一个块无条件的.
充其量,你可以改写相同的
if ( ( age1 == age2 ) && ( age2 == age3) ) { .... }
Run Code Online (Sandbox Code Playgroud)
在那之后,如果是
printf("All individuals have the same age of %d", &age1);
Run Code Online (Sandbox Code Playgroud)
你不需要传递变量的地址.事实上,这使得语句非常错误,将不兼容类型的参数传递给提供的转换说明符,这会导致未定义的行为.
| 归档时间: |
|
| 查看次数: |
99 次 |
| 最近记录: |