返回列表 發帖

if... else if... else 票價分類

假設有一遊樂園的票價表如下, 請設計一個小程式, 讓電腦依據使用者的年齡, 回應票價與種類:
       3歲以下   幼兒票   免費入場
  4歲 ~ 12歲   兒童票   50元
13歲 ~ 65歲   一般票   100元
     65歲以上   敬老票   70元

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.       int age;               
  6.       cout<<"請輸入你的年齡";
  7.       cin>>age;
  8.       cout<<""<<age<<"歲"<<endl;
  9.       if(age<=3)
  10.       {
  11.           cout<<"請購買幼兒票"<<endl;
  12.       }else if(age>=4 && age<=12)     
  13.       {
  14.           cout<<"請購買兒童票"<<endl;
  15.       }else if(age>=13 && age<=64)
  16.       {
  17.           cout<<"請購買一般票"<<endl;
  18.       }else if(age>=65)
  19.       {
  20.           cout<<"請購買敬老票"<<endl;
  21.       }
  22.       system("pause");
  23.       return 0;   

  24. }
複製代碼

TOP

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.       int score;               
  6.       cout<<"請輸入你的年齡:";
  7.       cin>>score;
  8.       cout<<"你的年齡是"<<score<<"歲"<<endl;
  9.       if(score>=0 && score<=3)
  10.       {
  11.           cout<<" 幼兒票   免費入場!!!"<<endl;
  12.       }else if(score>=4 && score<=12)     //&& ---> and
  13.       {
  14.           cout<<"兒童票   50元!"<<endl;
  15.       }else if(score>=13  && score<=65)
  16.       {
  17.           cout<<" 一般票   100元!"<<endl;
  18.       }else if(score>65)
  19.       {
  20.           cout<<" 敬老票   70元!!"<<endl;
  21.           }
  22.       system("pause");
  23.       return 0;
  24. }
複製代碼

TOP

  1. #include<iostream>

  2. using namespace std;

  3. int main()

  4. {

  5.       int age;               

  6.       cout<<"請輸入你的年齡";

  7.       cin>>age;

  8.       cout<<""<<age<<"歲"<<endl;

  9.       if(age<=3)

  10.       {

  11.           cout<<"請購買幼兒票"<<endl;

  12.       }else if(age>=4 && age<=12)     

  13.       {

  14.           cout<<"請購買兒童票"<<endl;

  15.       }else if(age>=13 && age<=64)

  16.       {

  17.           cout<<"請購買一般票"<<endl;

  18.       }else if(age>=65)

  19.       {

  20.           cout<<"請購買敬老票"<<endl;

  21.       }

  22.       system("pause");

  23.       return 0;   



  24. }
複製代碼

TOP

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.    int age;
  6.    cout<<"請輸入你的成績";
  7.    cin>>age;
  8.    cout<<""<<age"歲"endl;
  9.    {
  10.    if(age<=3)
  11.    {
  12.       cout<<"請購買幼兒票"<<endl;         
  13.    }else if(age>=4 && age<=12)
  14.    {
  15.      cout<<"請購買兒童票"<<endl;
  16.    }else if(age>=13 && age<=64)
  17.    {
  18.    cout<<"請購買一般票"<<endl;
  19.    }else if(age>=64)      
  20.    
  21.     cout<<"請敬老票"<<endl;
  22.     }
  23.     system("pause");
  24.     return 0;
  25.    
  26.    
  27.    
  28.    
  29.    
  30.    
  31.    
  32.    
  33.    
  34.    
  35.      
複製代碼

TOP

返回列表