2017-09-03 89 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; 
    } 
}