# 55屆公開第一題 ## 題目要求: (1) 由鍵盤輸入信用卡號碼之前兩位數字做為卡別判斷(使用 switch) 提示訊息【請輸入信用卡號碼前兩碼>】。 (2) 信用卡號前兩碼為 03,輸出【此卡片為聯合信用卡】。 信用卡號前兩碼為 35,輸出【此卡片為 JCB 信用卡】。 信用卡號前兩碼為 45,輸出【此卡片為 MASTER 信用卡】。 信用卡號前兩碼為 54,輸出【此卡片為 VISA 信用卡】。 若前兩碼在 30-39,區域【大中華區】。 若前兩碼在 40-49,區域【歐美地區】。 如皆不屬於上述所列,則輸出【非本店特約用戶】 ## 我的作法 ### 判斷卡號長度 使用者只會輸入兩位數字,如果不足兩位就顯示輸入格式錯誤 ```csharp if(input.Length ==2) { //略 } else { Console.Write("(輸入格式錯誤,請輸入兩位數字)"); } ``` ### 判斷卡別 我這裡將使用者輸入的數字用switch做判斷,當卡號等於題目所寫的卡號的時候再判斷卡號地區並一併顯示 ```csharp switch (Convert.ToInt32(input)) { case 03: //輸入為03 Console.WriteLine("此卡片為聯合信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if(Convert.ToInt32(input) >40&& Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; case 35: //輸入為35 Console.WriteLine("此卡片為 JCB 信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; case 45: //輸入為45 Console.WriteLine("此卡片為 MASTER 信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; case 54: //輸入為54 Console.WriteLine("此卡片為 VISA 信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; default: //非上方的數字 Console.WriteLine("非本店特約用戶"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; } ``` ### 完整程式碼 由於可以直接貼上使用,就不提供雲端檔案了 ```csharp Console.WriteLine("請輸入信用卡號碼前兩碼>"); string input = Console.ReadLine(); if(input.Length ==2) { switch (Convert.ToInt32(input)) //switch (比對的運算式) { case 03: Console.WriteLine("此卡片為聯合信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if(Convert.ToInt32(input) >40&& Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; case 35: Console.WriteLine("此卡片為 JCB 信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; case 45: Console.WriteLine("此卡片為 MASTER 信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; case 54: Console.WriteLine("此卡片為 VISA 信用卡"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; default: Console.WriteLine("非本店特約用戶"); if (Convert.ToInt32(input) > 30 && Convert.ToInt32(input) <= 39) { Console.WriteLine("(大中華區)"); } else if (Convert.ToInt32(input) > 40 && Convert.ToInt32(input) <= 49) { Console.Write("(歐美地區)"); } break; } } else { Console.Write("(輸入格式錯誤,請輸入兩位數字)"); } ``` ### 運行結果 ![image](https://hackmd.io/_uploads/SJ2Ijzmt1g.png) ![image](https://hackmd.io/_uploads/B15vsz7Yyx.png) ![image](https://hackmd.io/_uploads/r1TdjfXFJe.png)