#include <stdio.h>
int main()
{
int i; //counter for the loop
int n; //integer
int series;
printf("Enter an integer number: ");
scanf("%d" , &n);
for(i = 1; i <= n; i++)
{
if (i % 2 == 0)
(series -= i * i);
else
(series += i * i);
}
printf("The value of the series is: %d\n" , series);
return 0;
}
所以循環僅僅是一個基本的for循環中,利用i作爲計數器,只要它是不太大於或等於n試圖創建用於計算該系列的程序= 1^2 - 2^2 + 3^2
我必須複製的系列會添加奇數並減去偶數,因此if條件會測試數字是偶數還是奇數。該程序編譯罰款,但當我輸入整數爲5時,系列的總和應該是15,但是我的程序給出總和32779.任何幫助修復我的程序將不勝感激。
未初始化的變量'series'會造成的[未定義行爲 – John3136
可能重複發生在C聲明的,未初始化的變量是什麼?它是否有價值?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – John3136
Can請你澄清一下? – chrisHG