题目要求
运行效果
有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
{
[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;
}
}
}
|
请发表评论