2017-09-03 93 views
-4
/* 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) && (age2 == age3)){ 
     printf("All individuals have the same age of %d", &age2); 
    } 

    else (age1 != age2) && (age1 != age3) && (age2 != age3);{ 
     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; 

} 

嗨我改变了我的代码,但显示仍然显示一个奇怪的数字,当我输入每个人的相同年龄。我如何做到这一点,如果每个人都有同样的年龄,那么只有说所有人都有相同年龄的线。请帮助我作为它的分级任务,并且即时遇到这个问题排序3个人的年龄

所有人的年龄都是63567321是最年轻的。 1是中间的。 1是最古老的。

+1

'printf(“所有人的%d年龄都一样”,&age2);'Remove'&' - >'printf(“所有人的年龄都是%d \ n”,age2);' – BLUEPIXY

+2

请学习如何正确缩进你的代码,我个人甚至不会尝试阅读代码像那样呈现。然后,这不是一个问题,而是你的调试器。 –

+3

请勿[REPOST](https://stackoverflow.com/questions/45939540/sort-the-age-of-3-individuals)。 – gsamaras

回答

2

三个主要问题:

  1. 当平等的年龄,你是路过的指针整数整数本身printf()
  2. else statment没有一个'如果”
  3. 压痕 - 不会影响执行,但对帮助您更容易在代码中发现问题至关重要

正确的代码:

/* 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) && (age2 == age3)) 
    { 
    printf("All individuals have the same age of %d", age2); 
    } 

    else if ((age1 != age2) && (age1 != age3) && (age2 != age3)) 
    { 
     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; 

} 

- 编辑

请注意,在上述方案中,如果两个年龄都是平等的,最古老和最年轻的年龄不打印。要解决这个问题,我会删除else的if条件,并在打印中间值之前包含一个检查。新别的statment:

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); 

     // If two ages are equivalent, do not print middle 
     if ((age1 != age2) && (age1 != age3) && (age2 != age3)) 
      printf("%d is the middle.\n", middle); 

     printf("%d is the oldest.\n", oldest); 
    } 

免责声明:虽然我已经给出了上述解决方案,以保持原有的代码,我宁愿在下面的方式更通用的方法:

  1. 创建排序的方法整数数组:sort(int *array, int size)
  2. 读输入年龄为整数数组:int ages[3]
  3. 传千古的排序功能:sort(ages, 3);
  4. 从排序的阵列10个
  5. 打印结果:ages[0]ages[1]ages[2]

这样的代码的方式更具可读性和可重用..

+0

非常感谢你的帮助。到目前为止,我的课没有进展到学习数组,所以即时通讯使用如上所述的非常慢的方法。感谢您的建议和帮助! – Daniel

+0

我很乐意成为帮助:)请标记答案为已接受,如果你发现它有用:) –

0

你为什么不这样做

if (age1>age2 && age1>age3) { 
    oldest=age1; 
    if (age2>age3) 
     middle = age2; 
     youngest=age3; 
    } else { 
     middle = age3; 
     youngest=age2; 
    } 
} else if (age2>age3) { 
    oldest=age2; 
    if (age1>age3) 
     middle = age1; 
     youngest=age3; 
    } else { 
     middle = age3; 
     youngest=age1; 
    } 
} else { 
    oldest=age3; 
    if (age2>age1) 
     middle = age2; 
     youngest=age1; 
    } else { 
     middle = age1; 
     youngest=age2; 
    } 
}