新建窗體應用程序(如下),新建控件label1,label2,label3,textBOX1,button1,button2
label1的Text屬性改為“計算閏年演示”
label2的Text屬性改為“輸入年份”
button1的Text屬性改為“確定”
button1的Text屬性改為“退出”
完整代碼:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 計算閏年_窗體 12 { 13 public partial class Form1 : Form 14 { 15 //閏年:能被4整除,但不能被100整除或者能被400整除的年份 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void button2_Click(object sender, EventArgs e)//退出按鈕代碼 22 { 23 Application.Exit(); 24 } 25 26 private void button1_Click(object sender, EventArgs e)//確定按鈕代碼 27 { 28 try 29 { 30 int year = Convert.ToInt32(textBox1.Text); 31 if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)//閏年:能被4整除,但不能被100整除或者能被400整除的年份 32 { 33 label3.Text = string.Format("{0}是閏年", year);//輸出顯示閏年 34 } 35 else 36 { 37 label3.Text = string.Format("{0}不是閏年", year);//輸出顯示非閏年 38 } 39 } 40 catch 41 { 42 MessageBox.Show("請輸入正確年份"); 43 } 44 } 45 } 46 }