switch 與 if...else if..else 的轉換
本帖最後由 tonyh 於 2012-8-8 10:40 編輯
用 if...else if...else 語法, 將剛才利用 switch 寫的程式改寫.- #include<iostream>
- using namespace std;
- int main()
- {
- int answer;
- cout<<"***小米的腦殘問答***"<<endl;
- cout<<"猜猜看豬有幾隻腳? (1)一隻 (2)兩隻 (3)三隻 (4)四隻"<<endl;
- start:
- cout<<"請做答: ";
- cin>>answer;
- if(answer==1)
- {
- cout<<"用跳的啊?"<<endl;
- goto start;
- }
- else if(answer==2)
- {
- cout<<"沒吃過豬肉也看過豬走路!"<<endl;
- goto start;
- }
- else if(answer==3)
- {
- cout<<"你是豬啊! 笨!"<<endl;
- goto start;
- }
- else if(answer==4)
- {
- cout<<"終於答對了!"<<endl;
- }
- else
- {
- cout<<"別亂按!"<<endl;
- goto start;
- }
- system("pause");
- return 0;
- }
複製代碼 |