返回列表 發帖

for 迴圈 (五)

本帖最後由 tonyh 於 2012-8-9 09:34 編輯

在螢幕上顯示指定範圍內的連續數字, 讓使用者自行輸入初始值與終止值,
譬如當使用者分別輸入3與6時, 執行結果為  3 4 5 6
若輸入11與17時, 執行結果則為 11 12 13 14 15 16 17
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int x, y;
  6.     cout<<"請輸入初始值: ";
  7.     cin>>x;
  8.     cout<<"請輸入終止值: ";
  9.     cin>>y;
  10.     for(int i=x; i<=y; i++)
  11.     {
  12.          cout<<i<<" ";
  13.     }
  14.     cout<<endl;
  15.     system("pause");
  16.     return 0;   
  17. }
複製代碼

返回列表