最后一個輸出的平均值應該顯示 60,但我弄錯了。這里有什么錯誤?這是溫度輸入 72、46、90、20、70、85、60、40、-1000。
總的炎熱天數應該顯示 2,但在輸出上我得到 3。
#include <stdio.h> int categorize_days(int temp); int categorize_days(int temp){ if (temp >= 85){ return 1; } else if(temp >=60){ return 2; } else{ return 3; } } int main(){ int i, temp, h, p,c, temp_ave=0, type_days; double ave; printf("Lets check the whether !\n"); printf("Keep entering the integer temperature, enter -1000 to quit\n\n"); printf("Temperature: "); scanf("%d", &temp); while(temp!= -1000){ for(i = 0; i<8; i ){ temp_ave = temp; } type_days = categorize_days(temp); if( type_days == 1){ printf ("Day: h\n\n"); h ; } else if(type_days == 2){ printf ("Day: p\n\n"); p ; } else{ printf ("Day: c\n\n"); c ; } printf("Temperature: "); scanf("%d", &temp); } printf("End\n\n"); ave = temp_ave/8; printf("Total Hot days: %d \n", h); printf("Total Pleasant days: %d \n", p); printf("Total Cold days: %d \n", c); printf("Average temperature for 8 days is %f", ave); }
uj5u.com熱心網友回復:
第一個大錯誤在這里:
for(i = 0; i<8; i ){
temp_ave = temp; // You are not adding temp to temp_ave, to add you should
} // write temp_ave = temp;
// Anyway this won't help you, cause you are trying to add the same number 8 times.
// So therefore it will give you wrong average, when you are calculating it.
我想這就是你想做的。
#include <stdio.h>
int categorize_days(int temp) {
return (temp >= 85 ? 1 : (temp >= 60 ? 2 : 3));
}
int main() {
int temp = 0, h = 0, p = 0, c = 0, cnt = -1;
double ave = 0;
printf("Lets check the whether !\n");
printf("Keep entering the integer temperature, enter -1000 to quit\n\n");
while (temp != -1000) {
ave = temp;
cnt ;
printf("Temperature: ");
scanf("%d", &temp);
int type_days = categorize_days(temp);
if (type_days == 1) {
printf ("Day: h\n\n");
h ;
}
else if(type_days == 2) {
printf ("Day: p\n\n");
p ;
}
else {
printf ("Day: c\n\n");
c ;
}
}
printf ("End\n\n");
printf ("Total Hot days: %d \n", h);
printf ("Total Pleasant days: %d \n", p);
printf ("Total Cold days: %d \n", c);
printf ("Average temperature for 8 days is %f", ave / cnt);
}
uj5u.com熱心網友回復:
- 不要在代碼中使用幻數,跟蹤和更新變得乏味。根據需要使用
#define宏。
#define HOT_CUTOFF 85 // Fahrenheit
#define COLD_CUTOFF 60 // Fahrenheit
#define INPUT_GUARD -1000
- 利用
enum列出day-types:
enum {
eHotDay = 1,
eColdDay = 2,
eCozyDay = 3
} eDayType;
- 然后您
day_type()將更改為:
int day_type (const int temp) {
if (temp >= HOT_CUTOFF)
return eHotDay;
else if (temp <= COLD_CUTOFF) // should be less than or equal to
return eColdDay;
else
return eCozyDay;
}
- 始終在使用前初始化變數。
h,p&c在沒有初始化的情況下使用。此外,temp_ave保留總計的名稱是誤導性的。
int i, temp, h, p,c, temp_ave=0, type_days;
8當你-1000作為后衛時,為什么只限制輸入?
簡化:
#include <stdio.h>
#define HOT_CUTOFF 85 // Fahrenheit
#define COLD_CUTOFF 60 // Fahrenheit
enum {
eHotDay = 1,
eColdDay = 2,
eCozyDay = 3
} eDayType;
//int categorize_days (int temp); // redundant as you're defining the function before usage
//int categorize_days (int temp) {
int day_type (const int temp) {
if (temp >= HOT_CUTOFF)
return eHotDay;
else if (temp <= COLD_CUTOFF) // should be less than or equal to
return eColdDay;
else
return eCozyDay;
}
int main() {
printf ("Keep entering the integer temperature, enter -1000 to quit\n\n");
int total = 0;
int count = 0;
int hot = 0, cold = 0, cozy = 0;
while (1) {
int temp;
if (1 != scanf ("%d", &temp)) {
printf ("ERROR: Invalid input\n");
return 1;
}
if (INPUT_GUARD == temp) break;
total = temp;
count;
switch (day_type(temp)) {
case eHotDay : hot; break;
case eColdDay : cold; break;
case eCozyDay : cozy; break;
}
}
double avgTemp = 0.0;
if (count)
avgTemp = (double)total / count;
printf ("End\n\n");
printf ("Total Hot days: %d \n", hot);
printf ("Total Pleasant days: %d \n", cozy);
printf ("Total Cold days: %d \n", cold);
printf ("Average temperature for 8 days is %.2lf\n", avgTemp);
return 0;
}
uj5u.com熱心網友回復:
最后一個輸出的平均值應該顯示 60,但我弄錯了。
這個回圈是一團糟。一樣temp_ave = temp;,一樣temp_ave = temp;。
for (i = 0; i<8; i ) {
temp_ave = temp; // ????????
}
反而:
temp_ave = temp; // Note =
temp_ave/8在一個int部門。ave相反,根據避免強制轉換的型別執行除法。
// ave = temp_ave/8;
ave = temp_ave;
ave /= 8; // Division done per wider of type of `ave` and `int` constant `8`.
總的炎熱天數應該顯示 2,但在輸出上我得到 3。
啟用所有警告。
初始化h, p, c. @風向標
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464203.html
標籤:C
