[英]What's wrong with my printf output?
無法調試我在八月至十一月與十一月至十二月之間留出空間的原因。 這是代碼:
if (month == 1)
printf("jan");
if (month == 2)
printf("feb");
if (month == 3)
printf("march");
if (month == 4)
printf("apr");
if (month == 5)
printf("may");
if (month == 6)
printf("jun");
if (month == 7)
printf("jul");
if (month == 8)
printf("aug");
if (month == 9)
printf("sept");
if (month == 10)
printf("oct");
if (month == 11)
printf("nov");
if (month == 12)
printf("dec");
printf(" %c | %.1f | %.1f | %.1f | %.1f |\n", month, monthly->maxTemperature,
monthly->minTemperature, monthly->averageTemperature, monthly->totalPrecipitation);
也嘗試過更改間隔,但是出於某種原因,總是在兩個月之間進行更改。 謝謝!
編輯:
它是此功能的一部分:
void printMonthlyStatistic(int month,const struct MonthlyStatistic* monthly)
並在主程序中這樣調用:
for(i=0;i<12;i++){
printMonthlyStatistic(i+1,&monthly[i])
我的示例輸出:
| Month | High | Low | Avg | Precip |
|-----------|-------|-------|-------|---------|
jan | 9.8 | -26.2 | -7.8 | 55.3 |
feb | 7.5 | -23.3 | -8.6 | 33.1 |
march | 14.2 | -19.6 | -4.7 | 33.2 |
apr | 23.7 | -5.3 | 6.2 | 56.8 |
may | 33.0 | -0.6 | 13.9 | 62.7 |
jun | 32.1 | 8.0 | 19.7 | 69.7 |
jul | 34.9 | 12.6 | 22.2 | 181.8 |
aug | 31.5 | 11.0 | 20.9 | 69.2 |
sept | 34.1 | 5.0 | 16.1 | 69.0 |
oct
| 24.8 | -2.9 | 10.8 | 56.9 |
nov
| 16.0 | -12.8 | 2.1 | 36.2 |
dec
| 15.6 | -17.8 | -4.2 | 65.8 |
%c
打印具有指定字符的字符,如果使用ASCII碼,則10
為換行符, 11
為垂直制表符。
此外,你應該使用數組,而不是寫那些過於多的if
秒。
嘗試這個:
static const char *month_names[] = {"jan", "feb", "march", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec"};
printf("%-11s| %.1f | %.1f | %.1f | %.1f |\n",
1 <= month && month <= 12 ? month_names[month - 1] : "", monthly->maxTemperature,
monthly->minTemperature, monthly->averageTemperature, monthly->totalPrecipitation);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.