2016-09-21 85 views
-4

這可以做得更好嗎?

#include <stdio.h> 

int calculate_pizza_share(int number_of_people); 

int main() { 
    int num_of_people; 

    printf("how many people are shairng this pizza? "); 
    scanf("%d", &num_of_people); 

    calculate_pizza_share(num_of_people); 

    return 0; 
} 

int calculate_pizza_share(int number_of_people) { 
    if (number_of_people == 1) { 
    printf("You get %d slice(s) each\n", 8/number_of_people); 
    } else if (number_of_people == 2) { 
    printf("you get 4 slice(s) each\n"); 
    } else if (number_of_people == 3) { 
    printf("you get 2 slice(s) each\n"); 
    } else if (number_of_people == 4) { 
    printf("you get 2 slice(s) each\n"); 
    } else if (number_of_people == 5) { 
    printf("you get one slice(s) each\n"); 
    } else if (number_of_people == 6) { 
    printf("you get one slice(s) each\n"); 
    } else if (number_of_people == 7) { 
    printf("you get one slice(s) each\n"); 
    } else if (number_of_people == 8) { 
    printf("you get one slice(s) each\n"); 
    } 
} 
+1

莫非你請添加代碼? –

+1

'printf(「你得到%d片(s)每個\ n」,8/number_of_people);' –

+1

把代碼放在問題中,你爲什麼要發佈一個鏈接到你的編輯器的截圖? –

回答

2

如何一行:

printf("You get %d slice(s) each\n",8/number_of_people); 

你沒有得到的 「一」,而不是 「1」,但我認爲簡單將覆蓋。

的代稱,整個if/else 「階梯」 可以被替換爲switch/case


UPDATE:

固定我與編輯器的問題,代碼現在在該職位。

好的,這裏有兩種方法。我認爲單行仍然是最好的,但我已經取代了梯子,用switch

#include <stdio.h> 

int calculate_pizza_share(int number_of_people); 

int 
main() 
{ 

    int num_of_people; 

    printf("how many people are shairng this pizza? "); 
    scanf("%d", &num_of_people); 

    calculate_pizza_share(num_of_people); 

    return 0; 
} 

int 
calculate_pizza_share(int number_of_people) 
{ 

#ifndef SWITCH 
    printf("You get %d slice(s) each\n", 8/number_of_people); 
#else 
    switch (number_of_people) { 
    case 5: 
    case 6: 
    case 7: 
    case 8: 
     printf("You get one slice each\n"); 
     break; 
    default: 
     printf("You get %d slice(s) each\n", 8/number_of_people); 
     break; 
    } 
#endif 
} 
+0

請注意,如果你已經把代碼放在這個頁面上的代碼塊中,我會發佈一個'switch/case'例子,但是我沒有[並且很少有人會想要手工輸入。] –

0

有時候if/else if鏈條中去,產生涉及數字量的英語短語尤其是在正確的道路。但是想辦法減少條件的數量。在這個例子中,你真的只有三個(有效的)條件

  • 1人:獲得了整個比薩
  • 2,3或4人:整數運算滴師的小數部分,所以8/number_of_people會得到正確的答案,所有這些
  • 5,6,7,8:整數除法給出了正確的答案,但消息是不同的,所以對待這些作爲一個單獨的條件

此外,你應該總是假設用戶將輸入無意義(當有機會時)。所以,你需要從scanf檢查返回值,並準備處理是小於1且大於8

考慮到這一點的數字,這裏的代碼如下所示:

#include <stdio.h> 

void calculate_pizza_share(int number_of_people) 
{ 
    if (number_of_people < 1) 
     printf("You need some people to eat that pizza\n"); 
    else if (number_of_people == 1) 
     printf("You get the whole pizza\n"); 
    else if (number_of_people <= 4) 
     printf("You get %d slices each\n", 8/number_of_people); 
    else if (number_of_people <= 8) 
     printf("You get one slice each\n"); 
    else 
     printf("You need to order more pizza\n"); 
} 

int main(void) 
{ 
    int num_of_people; 

    printf("how many people are sharing this pizza? "); 

    if (scanf("%d", &num_of_people) != 1) 
     printf("Was hoping for a number between 1 and 8\n"); 
    else 
     calculate_pizza_share(num_of_people); 
} 
相關問題