推介:| 英語課程 | Causeway Bay order food | 職業英語 | 化妝課程 | Wedding Photography | 天窗卷布更換維修 |

發新話題
打印

變數PI的程式問題

變數PI的程式問題

以下為變數PI的程式設計的問題
#include<iostream>
#include<stdlib.h>
using namespace std;
const float PI = 3.14158;     // PI為全域變數
int main()
{
     int i=5;      // i 為局部變數
     {
        int j=10;    //  J為局部變數
        cout << 10*PI + i*j << '\n';
     }
     cout << j << '\n';   // 錯誤, 因j已經消失
     system("pause");
     return 0;
}
         

請留意一下第十二行:
cout << j << 'n';     // 錯誤, 因j已經消失是什麼意思
因為在Dev-C++中, 有關訊息是這樣的:
第十二行: '
第十二行:[each

TOP

Is it becuase you declare 'j' inside of the bracket?? I am not 100% sure... since I dont have a complier installed on my pc...

since you declare inside the bracket, this can be used within the barcket only....

int i=5;      // i 為局部變數
     {  <<-------------------------------------------------------- open bracket
        int j=10;    //  J為局部變數  <-------------------- variable you declare only works within brackets
        cout << 10*PI + i*j << '\n';
     }  <<-------------------------------------------------------- close bracket
     cout << j << '\n';   // 錯誤, 因j已經消失
     system("pause");
     return 0;

TOP

exactly, all the variables within the bracket will be destroyed ...

TOP

發新話題


重要聲明:本討論區是以即時上載留言的方式運作,本網站對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。本討論區有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿撰寫粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。


Copyright 1997- Xocat. All Right Reserved.