#include<iostream>
using namespace std;
int main(){
int i = 1;
int sum;
int N;
cout << "Enter a number N: ";
cin >> N;
while(i<=N)
{
if(i%2 == 0)
{
sum = sum i;
}
else
{
i = i 1;
}
}
cout << sum;
}
這是列印所有偶數的總和,直到 1 到 N。
當我嘗試運行代碼時,我被問到 N 的值,但前面沒有列印任何內容。
uj5u.com熱心網友回復:
對于初學者來說,變數sum沒有被初始化。
其次,i當它是偶數時,您還需要增加變數。所以回圈應該至少看起來像
while(i<=N)
{
if(i%2 == 0)
{
sum = sum i;
}
i = i 1;
}
一般來說,最好在使用變數的最小范圍內宣告變數。
因此,最好使用 for 回圈代替 while 回圈,例如
for ( int i = 1; i < N; i )
{
if ( i % 2 == 0 ) sum = i;
}
uj5u.com熱心網友回復:
while(i<=N)
{
if(i%2 == 0)
{
sum = sum i;
}
else
{
i = i 1;
}
}
讓我們一步一步來。想象一下,我們在回圈中i = 2,您已輸入N = 5. 在這種情況下...
while(i <= N)
2 <= 5 為真,所以我們回圈
if(i%2 == 0)
2 % 2 == 0 為真,所以我們進入這個分支
sum = sum i;
更新 sum,然后回到回圈的頂部
while(i <= N)
既沒有i也N沒有改變,所以 2 <= 5 仍然是真的。我們仍然回圈
if(i%2 == 0)
2 % 2 == 0 仍然為真,所以我們再次進入這個分支......
你看到這里發生了什么嗎?由于既不更新i也不N更新,您將繼續進入同一分支并無限回圈。你能想出一種方法來防止這種情況嗎?需要改變什么?
另請注意,這int sum;意味著sum將具有垃圾值(未初始化)。如果您希望它從 0 開始,則需要將其更改為
int sum = 0;
uj5u.com熱心網友回復:
當 i is even 因為你沒有增加它時,你會無限回圈。如果您想使用 while 回圈,更好的選擇是:
while(i<=N)
{
if(i%2 == 0)
sum = sum i;
i=i 1;
}
cout << sum;
如果在條件為假時不需要做任何事情,就不要使用 else。
uj5u.com熱心網友回復:
不需要回圈,如果需要,也可以在編譯時評估 sum
// use unsigned, the whole excercise is pointless for negative numbers
// use const parameter, is not intended to be changed
// constexpr is not needed, but allows for compile time evaluation (constexpr all the things)
// return type can be automatically deduced
constexpr auto sum_of_even_numbers_smaller_then(const unsigned int n)
{
unsigned int m = (n / 2);
return m * (m 1);
}
int main()
{
// compile time checking of the function
static_assert(sum_of_even_numbers_smaller_then(0) == 0);
static_assert(sum_of_even_numbers_smaller_then(1) == 0);
static_assert(sum_of_even_numbers_smaller_then(2) == 2);
static_assert(sum_of_even_numbers_smaller_then(3) == 2);
static_assert(sum_of_even_numbers_smaller_then(7) == 12);
static_assert(sum_of_even_numbers_smaller_then(8) == 20);
return 0;
}
uj5u.com熱心網友回復:
int main(){
int input; //stores the user entered number
int sum=0; //stroes the sum of all even numbers
repeat:
cout<<"Please enter any integer bigger than one: ";
cin>>input;
if(input<1) //this check the number to be bigger than one means must be positive integer.
goto repeat; // if the user enter the number less than one it is repeating the entry.
for(int i=input; i>0; i--){ // find all even number from your number till one and than totals it.
if(i%2==0){
sum=sum i;
int j=0;
j=j 1;
cout<<"Number is: "<<i<<endl;
}
}
cout<<endl<<"The sum of all even numbers is: "<<sum<<endl;}
復制此 C 代碼并運行它,它將解決您的問題。
uj5u.com熱心網友回復:
你的程式有兩個問題。
錯誤一
變數sum尚未初始化。這意味著它具有(持有)一個垃圾值。并且像您在撰寫時那樣使用這個垃圾值sum = sum i;是未定義的行為。
未定義的行為意味著任何1都可能發生,包括但不限于提供預期輸出的程式。但永遠不要依賴具有未定義行為的程式的輸出。
這就是為什么建議:
始終在本地/塊范圍內初始化內置型別。
錯誤2
第二個問題是您沒有更新variable 的值i。
解決方案
您可以解決這些問題,如下所示:
int main(){
int i = 1;
int sum = 0; //INITIALIZE variable sum to 0
int N;
cout << "Enter a number N: ";
cin >> N;
while(i<=N)
{
if(i%2 == 0)
{
sum = sum i;
}
i = i 1; //update(increase i)
}
cout << sum;
}
1有關未定義行為的更多閱讀(技術定義),您可以參考未定義行為的檔案,其中提到:對程式的行為沒有限制。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/396682.html
上一篇:函式需要一個回圈解決方案
