我正在制作秒表作业,我们必须制作自己的秒表课程;即我不能使用StopWatch。我有一个更新每秒的计时器,用于更新标签。我可以对我的班级做些什么,以便在我停止然后重新开始时保持经过的时间?目前它只是回到00:00:00。请记住,这是一项任务,这就是为什么它不是最合乎逻辑的方式。
public class Time
{
//fields
DateTime startTime, stopTime, currentTime;
bool isActive = false;
bool hasStarted = false;
public string ElapsedTime
{
get
{
// TODO: return something if never started..
if (isActive == false && hasStarted == false)
{
//Exception ex = new Exception("Please start the timer."); // This should be in the StartClock method.
//return ex.ToString();
return "00:00:00";
}
// TODO: return something if running...
else if (isActive == true && hasStarted == true)
{
return DateTime.Now.Subtract(startTime).ToString(@"hh\:mm\:ss");
}
// return if started then stopped (I did this one for you)
else
{
return (stopTime - startTime).ToString(@"hh\:mm\:ss");
}
}
}
public void StartClock() // Good! The "else" is where you should be throwing the exceptions
{
//TO DO: set the startTime
if(!isActive)
{
isActive = true;
hasStarted = true;
startTime = DateTime.Now;
}
else
{
Exception ex = new Exception("Please start the timer."); // This should be in the StartClock method.
}
}
public void StopClock()
{
//sets stop time
if(isActive && hasStarted)
{
isActive = false;
stopTime = DateTime.Now;//something else
}
}
}
答案 0 :(得分:0)
你可以尝试跟踪总的经过时间,这里有一些可能有帮助的伪代码:
this.elapseTime = 00:00:00;
startClicked: this.startedTime = Now()
stopClicked: this.elapsedTime + = Now() - this.startedTime
getCurrentElapsedTime: return this.elapsedTime + Now() - this.startedTime