C# 小例子

來源:互聯網
上載者:User

標籤:

 

輸入一個年份和月份輸出這個月份的天數

 1             Console.WriteLine("請輸入年份"); 2             try 3             { 4                 int year = Convert.ToInt32(Console.ReadLine()); 5                 Console.WriteLine("請輸入月份"); 6                 try 7                 { 8                     int month = Convert.ToInt32(Console.ReadLine()); 9                     if (month >= 1 && month <= 12)10                     {11                         int day = 0;12                         switch (month)13                         {14                             case 1:15                             case 3:16                             case 5:17                             case 7:18                             case 8:19                             case 10:20                             case 12: day = 31; break;21                             case 2: if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) day = 29; else day = 28; break;22                             default: day = 31; break;23                         }24                         Console.WriteLine("{0}年{1}月有{2}天", year, month, day);25                     }26                     else { Console.WriteLine("輸入的月份超出範圍"); }27                 }28                 catch { Console.WriteLine("輸入的月份有誤,程式退出"); }29             }30             catch { Console.WriteLine("輸入的年份有誤,程式退出"); }31             Console.ReadKey();
View Code

 不斷要求使用者輸入姓名,輸入q就結束

 1             string strname = ""; 2  3             while (strname != "q") 4             { 5                 Console.WriteLine("請輸入你的姓名,輸入q結束"); 6                 strname = Console.ReadLine(); 7             } 8             Console.ReadKey(); 9             //do...while10             /*do11             {12                 Console.WriteLine("請輸入你的姓名,輸入q結束");13                 strname = Console.ReadLine();14             } while (strname != "q");15             Console.ReadKey();*/
View Code

 不斷要求輸入一個數字(假定使用者輸入的是正整數),當使用者輸入end時顯示剛才輸入的數字中的最大值

 1             string input = ""; 2             int max = 0; 3             while (input != "end") 4             { 5                 Console.WriteLine("請輸入一個數字,輸入end將顯示輸入數中的最大值"); 6                 input = Console.ReadLine(); 7                 if (input != "end") 8                 { 9                     try10                     {11                         int number = Convert.ToInt32(input);12                         if (number > max)13                         {14                             max = number;15                         }16                     }17                     catch18                     {19                         Console.WriteLine("您收收入的字串有誤,請重新輸入");20                     }21                 }22                 else23                 {24                     Console.WriteLine("您剛才輸入的數字中最大值為{0}", max);25                 }26             } Console.ReadKey();
View Code

九九乘法表

1             for (int i = 1; i <= 9; i++)2             {3                 for (int j = 1; j <= i; j++)4                 {5                     Console.Write("{0}*{1}={2}\t", i, j, i * j);6                 } Console.WriteLine();7             } Console.ReadKey();
View Code

 用while continue實現計算1 到100(含)之間的除了能整除7以外的所有數的和

 1             int sum = 0; 2             int i = 1; 3             while (i <= 100) 4             { 5                 if (i % 7 == 0) 6                 { 7                     i++; 8                     continue; 9                 }10                 sum += i;11                 i++;12             }13             Console.WriteLine(sum);14             Console.ReadKey();
View Code

找出1到100之內所有的素數(質數)

 1             for (int i = 2; i <= 100; i++) 2             { 3                 bool b = true;//放在兩個迴圈之間,保證變數b為true 4                 for (int j = 2; j < i; j++) 5                 { 6                     //除盡說明不是質數,沒有再進行下去的必要 7                     if (i % j == 0) 8                     { 9                         b = false;10                         break;11                     }12                 }13                 if (b)14                 {15                     Console.WriteLine(i);16                 }17             } Console.ReadKey();
View Code

 

C# 小例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.