阿瑟 發表
if/else布林判別式讓您的程式可以在不同的狀況下有不同的輸出結果.
*注意: C++所有語法大小寫有差. 如果您在執行時發現中文無法顯示請自行將程式修改成英文.
#include <iostream.h> #include <stdlib.h> void main() { int computer; cin >> computer; if(computer > 0) { cout << "您輸入了一個正整數" << endl; } else if(computer < 0) { cout << "您輸入了一個負整數" << endl; } else { cout << "您輸入了 0, 既不是正數也不是負數" << endl; } } |
#include <iostream.h> #include <stdlib.h> void main() { int computer; cin >> computer; if(computer > 0) { cout << "您輸入了一個正整數" << endl; } if(computer > 3) { cout << "您輸入了一個大於三的正整數" << endl; } if(computer > 5) { cout << "您輸入了了一個大於五的正整數" << endl; } } |
#include <iostream.h> #include <stdlib.h> void main() { int computer; cin >> computer; if(computer > 0 && computer%2==0) { cout << "您輸入了一個正偶數" << endl; } else if(computer < 0 || computer==0) { cout << "你輸入的不是正整數" << endl; } else { cout << "你輸入了一個正奇數" << endl; } } |