#include <iostream>
using namespace std;
int main() {
int input = 0;
bool replay = false;
do {
cout << "輸入整數值:";
cin >> input;
cout << "輸入數為奇數?" << ((input % 2) ? 'Y': 'N') << "\n";
cout << "繼續(1:繼續 0:結束)?";
cin >> replay;
} while(replay);
return 0;
}
執行結果:
輸入整數值:11
輸入數為奇數?Y
繼續(1:繼續 0:結束)?0
如果您日後學會函式的使用,您還可以將這個迴圈寫的更漂亮一些,而不用使用replay變數,例如若下面more()函式詢問使用者是否繼續,如果是會傳回1,否則傳回0:
do {
cout << "輸入整數值:";
cin >> input;
cout << "輸入數為奇數?" << ((input % 2) ? 'Y': 'N') << "\n";
} while(more());
一個小小的改變,就可以使程式更為簡潔,邏輯上也較為清楚。
文章標籤
全站熱搜
