返回列表 發帖

五則運算 (三)

利用and 和 or 搭配if條件判斷來將五則運算練習的更完整
  1. #include<cstdlib>
  2. #include<iostream> // I (Input)O (Output) 串流
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y; //同時宣告x,y為int型別
  7.     //if 判斷式    如果.... 那就做什麼事 否則...就做甚麼事
  8.   
  9.     // ! => false
  10.     cout << "請輸入X的值\r\n";
  11.     cin >>  x;
  12.     cout << "請輸入y的值\r\n";
  13.     cin >> y;
  14.     // && => and  兩者的條件判斷都要為true的時候 才執行
  15.     // || => or   兩者的條件判斷只要一方為true 就會執行
  16.     if((x!=0) && (y!=0) )
  17.     {
  18.        cout << "當x的值為:" <<x <<";當y的值為:" << y <<endl;
  19.        cout << "x+y=" << x+y << endl;
  20.        cout << "x-y=" << x-y << endl;
  21.        cout << "x*y=" << x*y << endl;
  22.        cout << "x/y=" << x/y << endl;
  23.        cout << "x%y=" << x%y << endl;
  24.     }
  25.     else{
  26.         cout << "請重新輸入\r\n";
  27.    }

  28.    
  29.    
  30.    
  31.     system("pause");   
  32.     return 0;
  33. }
複製代碼

返回列表