<h2>信用卡號檢查</h2> <h3>程式要求:</h3> 驗證題目所要求的信用卡號 信用卡號為5181271099000012 在信用卡有效時顯示vaild 無效時顯示invaild <h3>方法:</h3> 程式要求要使用主控台,所以不需要layout 所以想要顯示就要用 ```csharp Console.WriteLine() ``` 程式分成計算和驗證兩部分 <h4>驗證</h4> 驗證就是把計算後的結果除以10判斷有沒有餘數 ```csharp int total = calculate(cardNumber); return total % 10 == 0; ``` <h4>計算</h4> 計算方式如下 a. 奇數位數數字乘以 2,所得之值依位數相加(A)。 b. 偶數位數數字相加(B)。 c. A+B 的個位數為 0 時為合法,否則不合法。 所以,我把卡號用char[]保存起來並反轉它 看起來像這樣: ```csharp char[] reversedCardNumber = cardNumber.ToCharArray(); Array.Reverse(reversedCardNumber); ``` 然後用for迴圈去判斷是奇數還偶數 ```csharp for (int i = 0; i < reversedCardNumber.Length; i++) ``` 註:.Length用於取得陣列或字串的長度 先把他從字符轉成字串再轉成整數 ```csharp int oppositecn = int.Parse(reversedCardNumber[i].ToString()); ``` 再把奇偶數分開計算 ```csharp if (i % 2 != 0) { a += y(oppositecn); } else { b += (oppositecn); } ``` ```csharp static int y(int oppositecn) { int ab = oppositecn * 2; return ab % 10 + ab / 10 ; } ``` 計算的完整過程 ```csharp static int calculate(string cardNumber) { int a = 0; int b = 0; char[] reversedCardNumber = cardNumber.ToCharArray(); Array.Reverse(reversedCardNumber); for (int i = 0; i < reversedCardNumber.Length; i++) { int oppositecn = int.Parse(reversedCardNumber[i].ToString()); if (i % 2 != 0) { a += y(oppositecn); } else { b += (oppositecn); } } return a + b; } static int y(int oppositecn) { int ab = oppositecn * 2; return ab % 10 + ab / 10 ; } ``` 接下來要判斷驗證結果是有效還無效 有效就顯示Valid 無效就顯示Invalid 所以我使用if判斷式判斷驗證出來的結果是有效還無效 ```csharp static void Main(string[] args) { string cn = "5181271099000012";//程式要求卡號 bool x = verify(cn);//驗證卡號 Console.WriteLine($"card number{cn}"); if (x == true) { Console.WriteLine($"Total {calculate(cn)} is valid."); } else { Console.WriteLine($"Total {calculate(cn)} is invalid."); } } ``` <h2> 問題 </h2> <h4> 1.最後加總的數字不對 </h4> 在計算那段程式裡,由於string[]裡的第一個值是0 所以我寫 i % 2 == 0 會變成奇偶數計算顛倒 所以要改成i % 2 != 0 計算才會是正確的 改正前: ```csharp if (i % 2 == 0) { a += y(oppositecn); } else { b += (oppositecn); } ``` 改正後: ```csharp if (i % 2 != 0) { a += y(oppositecn); } else { b += (oppositecn); } ``` <h4> 2.exe檔執行完會直接關閉 </h4> 因為不要讓程式執行完主控台直接關閉 所以在最後顯示時加上 ```csharp Console.WriteLine("按下任意鍵來結束程式..."); Console.ReadKey(true); ``` 讓使用者按下任意鍵在結束程式,並且一輸入就可以關閉視窗 <h2> 程式最終結果 </h2> ```csharp= namespace CSA04 { class Program { static void Main(string[] args) { string cn = "5181271099000012";//程式要求卡號 bool x = verify(cn);//驗證卡號 Console.WriteLine($"card number{cn}"); if (x == true) { Console.WriteLine($"Total {calculate(cn)} is valid."); } else { Console.WriteLine($"Total {calculate(cn)} is invalid."); } Console.WriteLine("按下任意鍵來結束程式..."); Console.ReadKey(true); } // 驗證信用卡號 static bool verify(string cardNumber) { int total = calculate(cardNumber); return total % 10 == 0; } // 計算卡號認證碼 (A+B) static int calculate(string cardNumber) { int a = 0; int b = 0; char[] reversedCardNumber = cardNumber.ToCharArray(); Array.Reverse(reversedCardNumber); for (int i = 0; i < reversedCardNumber.Length; i++)//.Length用於取得陣列或字串的長度 { int oppositecn = int.Parse(reversedCardNumber[i].ToString()); if (i % 2 != 0) { a += y(oppositecn); } else { b += (oppositecn); } } return a + b; } // 將數字乘以 2,並將結果的十位數和個位數相加 static int y(int oppositecn) { int ab = oppositecn * 2; return ab % 10 + ab / 10 ;//%10個位數,/10十位數 } }
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up