返回列表 發帖

[隨堂測驗] switch 判斷式 (五)

本帖最後由 鄭繼威 於 2022-12-30 20:11 編輯

最後一個switch-case練習
利用 switch 判斷式,完成如下之練習:



判斷條件為:
運動0~2天   有點少...加油!
運動3~4天   還不錯,繼續努力!
運動5~7天   健康寶寶代言人
不在以上範圍   你不知道一週有幾天嗎?

用switch-case判斷
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     //宣告變數
  6.     //變數型態 變數名字
  7.     int day;
  8.     cout<<"請輸入你運動的天數: ";
  9.     cin>>day;
  10.    
  11.     //switch-case判斷選項
  12.     switch(day){      //(要判斷的變數)
  13.            //case1
  14.            case 0 ... 2:
  15.                 cout<<"有點少...加油!"<<endl;
  16.                 break;
  17.            //case2
  18.            case 3 ... 4:
  19.                 cout<<"還不錯,繼續努力!"<<endl;
  20.                 break;
  21.                
  22.            //第三個case
  23.            case 5 ... 7:
  24.                 cout<<"健康寶寶代言人"<<endl;   
  25.                 break;
  26.            default:
  27.            cout<<"你不知道一週有幾天嗎?"<<endl;                           
  28.     }
  29.    
  30.     system("pause");
  31.     return 0;   
  32. }
複製代碼
用if-else判斷
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     //宣告變數
  6.     //變數型態 變數名字
  7.     int day;
  8.     cout<<"請輸入你運動的天數: ";
  9.     cin>>day;
  10.    
  11.     //if-else判斷選項
  12.     if(day>=0&&day<=2){
  13.          cout<<"有點少...加油!"<<endl;                              
  14.     }
  15.     else if(day>=3&&day<=4){
  16.          cout<<"還不錯,繼續努力!"<<endl;
  17.     }
  18.     else if(day>=5&&day<=7){
  19.          cout<<"健康寶寶代言人"<<endl;
  20.     }
  21.     else{
  22.          cout<<"你不知道一週有幾天嗎?"<<endl;        
  23.     }
  24.    
  25.     system("pause");
  26.     return 0;   
  27. }
複製代碼

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見
Attention Seeker </3

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

返回列表