我在c#中编了一个程序,结果感觉总是有一部分不能运行,不知道是怎么回事,求大神帮助
classTime{privateinthour;privateintminute;privateintsecond;privatevoidsetTime(inth,in...
class Time
{
private int hour;
private int minute;
private int second;
private void setTime(int h, int m, int s)//2
{
hour = h;
minute = m;
second = s;
}
public Time()//1
{
setTime(0, 0, 0);
}
public Time(int hourValue)
{
setTime(hourValue, 0, 0);
}
public Time(int hourValue, int minuteValue, int secondValue)
{
setTime(hourValue, minuteValue, secondValue);
}
public int Hour
{
get { return hour; }
set { hour = (value >= 0 && value <24 )? value : 0; }
}
public int Minute
{
get { return minute; }
set { minute = (value >= 0 && value < 60 )? value : 0; }
}
public int Second
{
get { return second; }
set { second = (value >= 0 && value < 60) ? value : 0; }
}
public string ToString24()
{
//int hourTemp = (Hour == 24) || (Hour == 0) ? 0 : (Hour % 24);
string output = Hour + ":" + minute + ":" + second;
return output;
}
public string ToString12()
{
int hourTemp = (Hour == 12) || (Hour == 0) ? 12 : (Hour % 12);
string AMPM = (Hour < 12 ? "AM" : "PM");
string output = hourTemp + ":" + Minute + ":" + second + AMPM;
return output;}}
class Program
{
static void Main(string[] args)
{Time time1 = new Time();
Time time2 = new Time(22);
Time time3 = new Time(24 , 61, 42);
Console.WriteLine("time1:");
Console.WriteLine("24小时制时间:" + time1.ToString24());
Console.WriteLine("12小时制时间:" + time1.ToString12());
Console.WriteLine("time2:");
Console.WriteLine("24小时制时间:" + time2.ToString24());
Console.WriteLine("12小时制时间:" + time2.ToString12());
Console.WriteLine("time3:");
Console.WriteLine("24小时制时间:" + time3.ToString24());
Console.WriteLine("12小时制时间:" + time3.ToString12());}}}
输入的时间超过或者等于24,我想让它等于0. 展开
{
private int hour;
private int minute;
private int second;
private void setTime(int h, int m, int s)//2
{
hour = h;
minute = m;
second = s;
}
public Time()//1
{
setTime(0, 0, 0);
}
public Time(int hourValue)
{
setTime(hourValue, 0, 0);
}
public Time(int hourValue, int minuteValue, int secondValue)
{
setTime(hourValue, minuteValue, secondValue);
}
public int Hour
{
get { return hour; }
set { hour = (value >= 0 && value <24 )? value : 0; }
}
public int Minute
{
get { return minute; }
set { minute = (value >= 0 && value < 60 )? value : 0; }
}
public int Second
{
get { return second; }
set { second = (value >= 0 && value < 60) ? value : 0; }
}
public string ToString24()
{
//int hourTemp = (Hour == 24) || (Hour == 0) ? 0 : (Hour % 24);
string output = Hour + ":" + minute + ":" + second;
return output;
}
public string ToString12()
{
int hourTemp = (Hour == 12) || (Hour == 0) ? 12 : (Hour % 12);
string AMPM = (Hour < 12 ? "AM" : "PM");
string output = hourTemp + ":" + Minute + ":" + second + AMPM;
return output;}}
class Program
{
static void Main(string[] args)
{Time time1 = new Time();
Time time2 = new Time(22);
Time time3 = new Time(24 , 61, 42);
Console.WriteLine("time1:");
Console.WriteLine("24小时制时间:" + time1.ToString24());
Console.WriteLine("12小时制时间:" + time1.ToString12());
Console.WriteLine("time2:");
Console.WriteLine("24小时制时间:" + time2.ToString24());
Console.WriteLine("12小时制时间:" + time2.ToString12());
Console.WriteLine("time3:");
Console.WriteLine("24小时制时间:" + time3.ToString24());
Console.WriteLine("12小时制时间:" + time3.ToString12());}}}
输入的时间超过或者等于24,我想让它等于0. 展开
2013-09-27
展开全部
你既然使用了属性来对设置的值进行强制,那么构造函数里也应该使用属性。
private void setTime(int h, int m, int s)//2
{
hour = h;
minute = m;
second = s;
}
你构造函数中调用了这个函数,这个函数直接对field进行设置值。
所以超过24(分秒是60)的值,你的强制没有发生作用。
应该改成:
private void setTime(int h, int m, int s)//2
{
Hour = h;
Minute = m;
Second = s;
}
private void setTime(int h, int m, int s)//2
{
hour = h;
minute = m;
second = s;
}
你构造函数中调用了这个函数,这个函数直接对field进行设置值。
所以超过24(分秒是60)的值,你的强制没有发生作用。
应该改成:
private void setTime(int h, int m, int s)//2
{
Hour = h;
Minute = m;
Second = s;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询