返回列表 發帖

五則運算 (三)

利用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. }
複製代碼

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
   
   
    int x;int y;
    cout<<"兩數的五則運算"<<endl;
    cout<<"x"<<endl;
    cin>>x;
    cout<<"y"<<endl;
    cin>>y;
   
    if(y==0)
    {
     cout<<"請重新輸入" <<endl;   
    }
    else{
    cout<<"x+y"<<x+y <<endl;
    cout<<"x-y"<<x-y <<endl;
    cout<<"x*y"<<x*y <<endl;
    cout<<"x/y"<<x/y <<endl;
    cout<<"x%y"<<x%y <<endl;
    system("pause");
    return 0;   }
    }

TOP

  1. #include<cstdlib>
  2. #include<iostream>
  3. using namespace std;
  4. int main()

  5. {
  6.     int x,y;
  7.    
  8.     cout<<"請輸入x的值"<<endl;
  9.     cin>>x;
  10.     cout<<"請輸入y的值"<<endl;
  11.     cin>>y;
  12.     if((x!=0)&&(y!=0))
  13.     {
  14.           cout<<"當x的質為:"<<x;cout<<"當y的質為:"<<y<<endl;
  15.           cout<<"x+y="<<x+y<<endl;
  16.           cout<<"x-y="<<x-y<<endl;
  17.           cout<<"x*y="<<x*y<<endl;
  18.           cout<<"x/y="<<x/y<<endl;
  19.           cout<<"x%y="<<x%y<<endl;   
  20.             
  21.     }
  22.     else
  23.     {
  24.             cout<<"請重新輸入"<<endl;
  25.    
  26.     }
  27.             
  28.    

  29.          
  30.             
  31.    
  32.    
  33.    
  34.          
  35.    
  36.    
  37.     system("pause");
  38.     return 0;
  39. }
複製代碼

TOP

  1. #include<cstdlib>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y;
  7.     cout<<"請輸入x的值:";
  8.     cin>>x;
  9.     cout<<"請輸入y的值:";
  10.     cin>>y;
  11.     if((x!=0)&&(y!=0))
  12.     {
  13.     cout<<"x="<<x<<",y="<<y<<endl;
  14.     cout<<"x+y="<<x+y<<endl;
  15.     cout<<"x-y="<<x-y<<endl;
  16.     cout<<"x*y="<<x*y<<endl;
  17.     cout<<"x/y="<<x/y<<endl;
  18.     cout<<"x%y="<<x%y<<endl;        
  19.     }else
  20.     {
  21.     cout<<"請重新輸入"<<endl;   
  22.     }        
  23.     system("pause");
  24.     return 0;
  25. }
複製代碼

TOP

  1. #include<cstdlib>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.       int x,y;
  7.        cout<<"輸入x的值"<<endl;
  8.        cin>>x;
  9.        cout<<"輸入y的值"<<endl;
  10.        cin>>y;
  11.       if(!(x==0)&&!(y==0))
  12.       {
  13.        cout<<"當x的值為:"<<x<<"當y的值為:"<<y<<endl;
  14.        cout<<"x+y="<<x+y<<endl;
  15.        cout<<"x-y="<<x-y<<endl;
  16.        cout<<"x*y="<<x*y<<endl;
  17.        cout<<"x/y="<<x/y<<endl;
  18.        cout<<"x%y="<<x%y<<endl;
  19.       }
  20.       else
  21.       {
  22.        cout<<"請重新輸入"<<endl;
  23.        }
  24.       system("pause");
  25.       return 0;
  26. }
複製代碼

TOP

返回列表