关于int和string的转化

来源:百度知道 编辑:UC知道 时间:2024/06/16 01:56:03
private void timer1_Tick(object sender, EventArgs e)
{
string h = DateTime.Now.Hour.ToString();//获取系统时间小时
string m = DateTime.Now.Minute.ToString();//获取系统时间分钟
string s = DateTime.Now.Second.ToString();//获取系统时间秒
if (s.Length < 2) {
s = "0" + s;
}
if (m.Length < 2)
{
s = "0" + s;
}

if (h < 7) {
h = "清晨" + h;
}
if (h < 12) {
h = "中午" + h;
}
if (h < 18) {
h = "下午" + h;
}

label10.Text = "" + h + ":" + m + ":" + s + "";

}
if (h < 7) {
h = "清晨" + h;

int hour=DateTime.Now.Hour;
string h = DateTime.Now.Hour.ToString();//获取系统时间小时
string m = DateTime.Now.Minute.ToString();//获取系统时间分钟
string s = DateTime.Now.Second.ToString();//获取系统时间秒
if (s.Length < 2) {
s = "0" + s;
}
if (m.Length < 2)
{
s = "0" + s;
}

if (hour < 7) {
h = "清晨" + h;
}
else if (hour < 12) {
h = "中午" + h;
}
else if (hour < 18) {
h = "下午" + h;
}
else
h = "晚上" + h;
this.textBox1.Text=h;

这样写吧
最后加一个晚上

把int转换成string,使用 Convert.ToString(h);
把sting转换成int,使用Convert.ToInt32(h);
或者可以显式转换 (string) h;

if (Convert.ToInt32(h) < 7)
{

(string)h = "清晨" + (string)h ;
}

可以使用Integer.parseInt()方法,讲字符串转换为对应的整数。
if (Integer.parseInt(h) < 7) {
h = "清晨&