2017.02.24C# 跳躍陳述式,迭代法,窮舉法,異常語句處理。

來源:互聯網
上載者:User

標籤:運行   stat   這一   name   nal   .text   color   span   eric   

一,跳躍陳述式
(1)break:

代碼:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace @break 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             Int32 a = 0;13                 for(a=0;a<25;a++)14                 {15                     Console.WriteLine(a);16                     if (a==10)17                         18                    break;19                     20                     21                 }22 23                 Console.ReadLine();24         }25     }26 }

 

如果沒有break,代碼從1一直輸出到999,但break的出現打斷了,當a=10 的時候,跳出輸出,到10就中斷了,只輸出到10,break的作用就是跳出所作用地區的迴圈,終止作用地區的迴圈。


(2)continue
代碼:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace @break 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             Int32 a = 0;13                 for(a=0;a<25;a++)14                 {15                     16                     if (a==10)17                   18                         continue;19                     Console.WriteLine(a);20                 }21 22                 Console.ReadLine();23         }24     }25 }

 

 

這段代碼輸出的結果是0---24,中間沒有10,出現10的時候跳出了輸出10,但是還繼續進行輸出11的迴圈,故輸出結果沒有10.continue的作用是當出現這個情況時,跳出這一次迴圈,並繼續下一個迴圈。


二,窮舉法
代碼:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace 窮舉法 7 { 8     class Program 9     {10         static void Main(string[] args)11         {//100元,手套5元,襪子2元,共有幾種情況12             Int32 a = 0;13             Int32 b = 0;14             for (a = 0; a <= 20; a++)15             {16                 for (b = 0; b <= 50; b++)17                 {18                     if ((a * 5) + (b * 2) == 100)19                     {20                         Console.WriteLine("可買" + a + "只手套," + b + "只襪子。");21                     }22 23                 }24             }25 26             Console.ReadLine();27 28 29 30 31 32 33 34         }35     }36 }

 

這段代碼輸出了買多少只襪子和手套有多少情況,分別是什麼。
窮舉法就是把有多少種情況都列舉出來。

三,異常語句處理

try catch


代碼

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace 作業 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             for (; ; )13             { Console.WriteLine("請輸入數字");14                15                String b=Console.ReadLine();16                try17                {18                    Int32 c = Convert.ToInt32(b);19                    20                    Console.WriteLine("輸入的是數字,這次就饒了你");21                    break;22                }23                catch24                {25                    Console.WriteLine("輸入的不是數字,重新輸入!");26                }27 28             }29             Console.ReadLine();30         }31     }32 }

 

這段代碼意思是使用者輸入數字,輸入其他提示錯誤,讓使用者重新輸入,直到使用者輸入正確數字才會結束迴圈。
try{代碼可能正確,也可能出現錯誤,如果正確,繼續運行,如果錯誤運行catch}
catch{try出現錯誤運行這裡}
finally{無論對錯都會運行這裡}

2017.02.24C# 跳躍陳述式,迭代法,窮舉法,異常語句處理。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.