September 27, 2012

Yo~

#include

using namespace std;

int main()
{
    int num,b; //宣告兩個變數 一個讓別人指定 一個拿來當對2取餘數的值
    cout << "Enter an integer: ";
    cin >> num;
    b=num%2; //讓b的值是num除以2的餘數

    if(b==1) // if b等於1的狀況 就執行下面(if)的指令
    cout << "The integer " << num << " is odd.";
    else // 除了if的條件成立之外的所有狀況 就執行下面(else)的指令
    cout << "The integer " << num << " is even.";

    return 0;
}






//之後的中文是我的註解 不是程式碼的一部分喔!
如果是用一個一個if的話 就必須把所有狀況考慮進去
以這個例子來說 b有0和1兩種狀況
所以在算完b=num%2之後
就會變成 


if(b==1) // b等於1的狀況 就執行下面(if)的指令
cout << "The integer " << num << " is odd.";
if(b==0) // b等於0的狀況 就執行下面(if)的指令
cout << "The integer " << num << " is even.";


這樣子打
假如今天b有十種狀況 我就要打十個if
else的話 就是除了成立的狀況之外的所有狀況
比較方便好用 也不會有打入數字跑不出來的狀況囉:D
(當然某些情況還是可以if 連用 只是比較累而已)

不懂的話可以再問我^___^

Today's Visitors: 0 Total Visitors: 10
Personal Category: Uncategorized Articles Topic: feeling / personal / murmur
[Trackback URL]

Reply
  • 1樓

    1樓搶頭香

    Sealed

  • Sealed at September 27, 2012 09:42 PM comment
Post A Comment









Yes No



Please input the magic number:

( Prevent the annoy garbage messages )
( What if you cannot see the numbers? )
Please input the magic number

誰來收藏
Loading ...
unlog_NVPO 0