C# 实验四 获取系统时间、点击加一秒功能
生活随笔
收集整理的這篇文章主要介紹了
C# 实验四 获取系统时间、点击加一秒功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目要求
運(yùn)行效果
有60秒進(jìn)位,60分鐘進(jìn)位,24小時(shí)進(jìn)位(清零)功能
代碼
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace 第五章_顯示時(shí)間 {public partial class Form1 : Form{public Form1(){InitializeComponent();Time time = new Time();//輸出當(dāng)前時(shí)間textBox1.Text = time.Hour.ToString();textBox2.Text = time.Min.ToString();textBox3.Text = time.Sec.ToString();}private void button1_Click(object sender, EventArgs e){//獲取文本int sec = int.Parse(textBox3.Text);int min = int.Parse(textBox2.Text);int hour = int.Parse(textBox1.Text);//計(jì)算進(jìn)位sec += 1;if (sec >= 60){sec = 0;min += 1;if (min >= 60){min = 0;hour += 1;if (hour >= 24){hour = min = sec = 0;}}}//輸出文本textBox1.Text = hour.ToString();textBox2.Text = min.ToString();textBox3.Text = sec.ToString();}} }Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms;namespace 第五章_顯示時(shí)間 {static class Program{/// <summary>/// 應(yīng)用程序的主入口點(diǎn)。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}}public class Time{//只讀屬性public int Hour { get; }public int Min { get; }public int Sec { get; }//構(gòu)造函數(shù)public Time(){Hour = System.DateTime.Now.Hour;Min = System.DateTime.Now.Minute;Sec = System.DateTime.Now.Second;}//重載public Time(int h, int m, int s){Hour = h;Min = m;Sec = s;}} }總結(jié)
以上是生活随笔為你收集整理的C# 实验四 获取系统时间、点击加一秒功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Verilog语法】分支延迟槽
- 下一篇: vb for循环 combobox的使用