这段代码运行没有问题,就是在给文本框中输入值后,单击按钮,Label1就显示空白了。
我是以,20,56,30,带进去的,各位帮忙看下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace again
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ time Atime = new time();
Atime.Sethour(Int32.Parse(textBox1.Text));
Atime.Setminute(Int32.Parse(textBox2.Text));
Atime.Setsecond(Int32.Parse(textBox3.Text));
string s = Atime.Gettimes();
label1.Text = s;
}
}
public class time
{
private int Hour;
private int Minute;
private int Second;
private string times;
public int Gethour()
{
return Hour;
}
public void Sethour(int Newhour)
{
Hour = (Hour > 0 && Hour < 24) ? Hour : -1;
}
public int Getminute()
{
return Minute;
}
public void Setminute(int Newminute)
{
Minute = (Minute >= 0 && Minute < 60) ? Minute : -1;
}
public int Getsecond()
{
return Second;
}
public void Setsecond(int Newsecond)
{
Second = (Second >= 0 && Second < 60) ? Second : -1;
}
public string Gettimes()
{
return times;
}
public void Settimes(int Newtimes)
{
if (Hour != -1 && Minute != -1 && Second != -1)
{
times =String.Format("{0}:{1:D2}:{2:D2} {3}",
(Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
Hour < 12 ? "AM" : "PM");
}
else
MessageBox.Show("你输入的时间不正确,请重新输入");
}
}
}
我是以,20,56,30,带进去的,各位帮忙看下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace again
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ time Atime = new time();
Atime.Sethour(Int32.Parse(textBox1.Text));
Atime.Setminute(Int32.Parse(textBox2.Text));
Atime.Setsecond(Int32.Parse(textBox3.Text));
string s = Atime.Gettimes();
label1.Text = s;
}
}
public class time
{
private int Hour;
private int Minute;
private int Second;
private string times;
public int Gethour()
{
return Hour;
}
public void Sethour(int Newhour)
{
Hour = (Hour > 0 && Hour < 24) ? Hour : -1;
}
public int Getminute()
{
return Minute;
}
public void Setminute(int Newminute)
{
Minute = (Minute >= 0 && Minute < 60) ? Minute : -1;
}
public int Getsecond()
{
return Second;
}
public void Setsecond(int Newsecond)
{
Second = (Second >= 0 && Second < 60) ? Second : -1;
}
public string Gettimes()
{
return times;
}
public void Settimes(int Newtimes)
{
if (Hour != -1 && Minute != -1 && Second != -1)
{
times =String.Format("{0}:{1:D2}:{2:D2} {3}",
(Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
Hour < 12 ? "AM" : "PM");
}
else
MessageBox.Show("你输入的时间不正确,请重新输入");
}
}
}
代码有问题:问题一:
Atime.Sethour(Int32.Parse(textBox1.Text));
Atime.Setminute(Int32.Parse(textBox2.Text));
Atime.Setsecond(Int32.Parse(textBox3.Text));
这三行代码传的参数实际上都没有用到
问题二:
string s = Atime.Gettimes(); 这个函数返回times,但是times一直没有赋过值
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace again
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
time Atime = new time();
Atime.Sethour(Int32.Parse(textBox1.Text));
Atime.Setminute(Int32.Parse(textBox2.Text));
Atime.Setsecond(Int32.Parse(textBox3.Text));
//实例化一个类后,你要给你的times赋值啊
Atime.Settimes(); //你缺少的是这句话
string s = Atime.Gettimes();
label1.Text = s;
}
}
public class time
{
private int Hour;
private int Minute;
private int Second;
private string times;
public int Gethour()
{
return Hour;
}
public void Sethour(int Newhour)
{
Hour = (Hour > 0 && Hour < 24) ? Hour : -1;
}
public int Getminute()
{
return Minute;
}
public void Setminute(int Newminute)
{
Minute = (Minute >= 0 && Minute < 60) ? Minute : -1;
} public int Getsecond()
{
return Second;
} public void Setsecond(int Newsecond)
{
Second = (Second >= 0 && Second < 60) ? Second : -1;
}
public string Gettimes()
{
return times;
} public void Settimes()
{
if (Hour != -1 && Minute != -1 && Second != -1)
{
times =String.Format("{0}:{1:D2}:{2:D2} {3}",
(Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
Hour < 12 ? "AM" : "PM");
}
else
{
MessageBox.Show("你输入的时间不正确,请重新输入");
}
}
}
}
{
if (Hour != -1 && Minute != -1 && Second != -1)
{
times =String.Format("{0}:{1:D2}:{2:D2} {3}",
(Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
Hour < 12 ? "AM" : "PM");
}
else
MessageBox.Show("你输入的时间不正确,请重新输入");
return times;
}
public void Settimes()
{
if (Hour != -1 && Minute != -1 && Second != -1)
{
times =String.Format("{0}:{1:D2}:{2:D2} {3}",
(Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
Hour < 12 ? "AM" : "PM");
}
else
{
MessageBox.Show("你输入的时间不正确,请重新输入");
}
}
已帮你改正,将如下代码拷贝过去覆盖下试试吧。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace again
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
time Atime = new time();
Atime.Sethour(Int32.Parse(textBox1.Text));
Atime.Setminute(Int32.Parse(textBox2.Text));
Atime.Setsecond(Int32.Parse(textBox3.Text));
//实例化一个类后,你要给你的times赋值啊
Atime.Settimes(); //你缺少的是这句话
string s = Atime.Gettimes();
label1.Text = s;
}
}
public class time
{
private int Hour;
private int Minute;
private int Second;
private string times;
public int Gethour()
{
return Hour;
}
public void Sethour(int Newhour)
{
Hour = (Newhour> 0 && Newhour< 24) ? Newhour: -1;
}
public int Getminute()
{
return Minute;
}
public void Setminute(int Newminute)
{
Minute = (Newminute>= 0 && Newminute< 60) ? Newminute: -1;
} public int Getsecond()
{
return Second;
} public void Setsecond(int Newsecond)
{
Second = (Newsecond>= 0 && Newsecond< 60) ? Newsecond: -1;
}
public string Gettimes()
{
return times;
} public void Settimes()
{
if (Hour != -1 && Minute != -1 && Second != -1)
{
times =String.Format("{0}:{1:D2}:{2:D2} {3}",
(Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
Hour < 12 ? "AM" : "PM");
}
else
{
MessageBox.Show("你输入的时间不正确,请重新输入");
}
}
}
}