C++ 入門指南 4.01
練習 7.8 參考程式 - 練習 if-else 陳述
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// std 為標準程式庫的命名空間
using namespace std;
int main(void) {
// 宣告整數變數 a
int a;
// 取得使用者輸入
cin >> a;
// 利用 if-else 判斷整數變數 a
if (a == 0) {
cout << "0" << endl;
}
else if (a == 1) {
cout << "1" << endl;
}
else if (a == 2) {
cout << "2" << endl;
}
else if (a == 3) {
cout << "3" << endl;
}
else if (a == 4) {
cout << "4" << endl;
}
else if (a == 5) {
cout << "5" << endl;
}
else if (a == 6) {
cout << "6" << endl;
}
else if (a == 7) {
cout << "7" << endl;
}
else if (a == 8) {
cout << "8" << endl;
}
else if (a == 9) {
cout << "9" << endl;
}
else if (a == 0) {
cout << "0" << endl;
}
else {
cout << "a > 10 || a < 0" << endl;
}
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise0708.cxx
編譯:g++ exercise0708.cxx
執行:./a.out
功能:C++入門指南單元七的練習
作者:張凱慶 */
回到練習題目