我一直在這個代碼附近玩了幾個小時,儘管很簡單,但我找不到它有什麼問題。這是邏輯嗎?或者問題是與語法相關的問題?計數器和累加器無法正常工作,導致程序崩潰。我究竟做錯了什麼?
我希望程序要求用戶輸入一個數字,指出他們本月在比賽中個人跑完了多少公里。 該方案將平均告訴他們每場比賽他們跑了多少。
事不宜遲,下面的代碼:
#include <stdio.h>
main()
{
int STOP_VALUE = 8 ; /* you pick this number - outside the valid data set */
int avg;
int currentItem;
float runningTotal = 0 ;
int counterOfItems = 0 ;
printf("Enter first item or 8 to stop: ");
scanf("%d", ¤tItem);
while (currentItem != 8) {
runningTotal += currentItem;
++counterOfItems;
printf("Enter next item or 8 to stop: ");
scanf("%d", currentItem);
}
/* protect against division by 0 */
if (counterOfItems != 0)
{
avg = runningTotal/counterOfItems ;}
else {
printf("On average, you've run %f per race and you've participated in %f running events. Bye! \n", runningTotal, counterOfItems);
}
return 0;
}
預期產量是多少?你會得到什麼? – DyZ