• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# 实验四 获取系统时间、点击加一秒功能

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

题目要求

运行效果

有60秒进位,60分钟进位,24小时进位(清零)功能

代码

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 第五章_显示时间
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Time time = new Time();
            //输出当前时间
            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);

            //计算进位
            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 第五章_显示时间
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </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; }

        //构造函数
        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;
        }
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#图片处理发布时间:2022-07-10
下一篇:
C#语言基础——数组发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap