C# 程序中的负毫秒数
大家好,我正试图在Android Xamarin上做类似的事情:。所以在第一次点击之后,毫秒的返回值是负数,比如-525。点击几下后,它会变得越来越高,直到最后变成正数。有人能解释一下为什么是否定的吗?计时器运行时间基本上是指绿色屏幕在随机时间内弹出。弹出后,我的毫秒计数开始。当点击绿色按钮时,它就完成了。我非常感谢您的帮助。这就是问题所在:C# 程序中的负毫秒数,c#,xamarin,C#,Xamarin,大家好,我正试图在Android Xamarin上做类似的事情:。所以在第一次点击之后,毫秒的返回值是负数,比如-525。点击几下后,它会变得越来越高,直到最后变成正数。有人能解释一下为什么是否定的吗?计时器运行时间基本上是指绿色屏幕在随机时间内弹出。弹出后,我的毫秒计数开始。当点击绿色按钮时,它就完成了。我非常感谢您的帮助。这就是问题所在: //Global variables bool pause; TextView ClickToStart;
//Global variables
bool pause;
TextView ClickToStart;
TextView ClickAsSoonAsPossible;
TextView Tries;
TextView TriesCount;
TextView AverageMs;
TextView AverageMsCount;
Button ScreenClickButton;
System.Timers.Timer timer1;
Color currentColor = Color.White;
Random rnd = new Random();
DateTime startTime1;
DateTime endTime1;
int secondsToFormat;
int ClicksCount = 0;
private void Timer1_Elapsed(object sender, ElapsedEventArgs e)
{
RunOnUiThread(() =>
{
Toast.MakeText(this, "Timer", ToastLength.Short).Show();
currentColor = Color.Green;
ScreenClickButton.SetBackgroundColor(currentColor);
});
timer1.Stop();
startTime1 = DateTime.Now;
}
private void ClickCount(object sender, EventArgs e)
{
if (currentColor == Color.White)
{
currentColor = Color.Red;
ScreenClickButton.SetBackgroundColor(currentColor);
int seconds = rnd.Next(1, 5);
Toast.MakeText(this, "Seconds:" + seconds, ToastLength.Short).Show();
timer1.Interval = seconds * 1000;
timer1.Start();
}
else if (currentColor == Color.Green)
{
endTime1 = DateTime.Now;
ClicksCount++;
secondsToFormat =endTime1.Millisecond - startTime1.Millisecond;
AverageMsCount.Text = secondsToFormat.ToString();
// if(secondsToFormat>=1000)
// {
// AverageMs.Text = ((endTime1 - startTime1).Milliseconds).ToString();
// }
currentColor = Color.White;
ScreenClickButton.SetBackgroundColor(currentColor);
}
else if (currentColor == Color.Red)
{
currentColor = Color.White;
ScreenClickButton.SetBackgroundColor(currentColor);
}
//Toast.MakeText(this, "You clicked me " + ClicksCount + "times", ToastLength.Short).Show();
if (ClicksCount == 1)
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "1";
if (ClicksCount == 2)
{
secondsToFormat = secondsToFormat / 2;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "2";
}
if (ClicksCount == 3)
{
secondsToFormat = secondsToFormat / 3;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "3";
}
if (ClicksCount == 4)
{
secondsToFormat = secondsToFormat / 4;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "4";
}
if (ClicksCount == 5)
{
secondsToFormat = secondsToFormat / 5;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "5";
}
if (Click`enter code here`sCount == 6)
{
ClicksCount = 1;
}
}
毫秒
属性是“秒内的毫秒”。因此,假设我们有:
secondsToFormat = endTime1.Millisecond - startTime1.Millisecond;
。。。这将给您一个-650ms的结果,因为startTime1.millished
是100,endTime1.millished
是750
理想情况下:
- 根本不要使用
:使用DateTime
,只要您想开始计时,就可以重置并启动它,然后只要您想知道经过了多少时间,就可以使用Stopwatch
属性获取appeased
。使用该TimeSpan
的TimeSpan
属性来确定经过了多少毫秒。(或者,使用totalmillizes
)秒表.ElapsedMilliseconds
- 如果您必须继续使用
DateTime
- 使用
而不是DateTime.UtcNow
,这样,如果您的用户在时区偏移更改边界(例如夏令时)上运行此操作,您不会得到奇怪的结果DateTime.Now
- 将两个值之间的差值作为
,例如TimeSpan
,然后再次使用endTime1-startTime1
属性totalmillizes
- 使用
- 这就是问题所在:
//Global variables
bool pause;
TextView ClickToStart;
TextView ClickAsSoonAsPossible;
TextView Tries;
TextView TriesCount;
TextView AverageMs;
TextView AverageMsCount;
Button ScreenClickButton;
System.Timers.Timer timer1;
Color currentColor = Color.White;
Random rnd = new Random();
DateTime startTime1;
DateTime endTime1;
int secondsToFormat;
int ClicksCount = 0;
private void Timer1_Elapsed(object sender, ElapsedEventArgs e)
{
RunOnUiThread(() =>
{
Toast.MakeText(this, "Timer", ToastLength.Short).Show();
currentColor = Color.Green;
ScreenClickButton.SetBackgroundColor(currentColor);
});
timer1.Stop();
startTime1 = DateTime.Now;
}
private void ClickCount(object sender, EventArgs e)
{
if (currentColor == Color.White)
{
currentColor = Color.Red;
ScreenClickButton.SetBackgroundColor(currentColor);
int seconds = rnd.Next(1, 5);
Toast.MakeText(this, "Seconds:" + seconds, ToastLength.Short).Show();
timer1.Interval = seconds * 1000;
timer1.Start();
}
else if (currentColor == Color.Green)
{
endTime1 = DateTime.Now;
ClicksCount++;
secondsToFormat =endTime1.Millisecond - startTime1.Millisecond;
AverageMsCount.Text = secondsToFormat.ToString();
// if(secondsToFormat>=1000)
// {
// AverageMs.Text = ((endTime1 - startTime1).Milliseconds).ToString();
// }
currentColor = Color.White;
ScreenClickButton.SetBackgroundColor(currentColor);
}
else if (currentColor == Color.Red)
{
currentColor = Color.White;
ScreenClickButton.SetBackgroundColor(currentColor);
}
//Toast.MakeText(this, "You clicked me " + ClicksCount + "times", ToastLength.Short).Show();
if (ClicksCount == 1)
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "1";
if (ClicksCount == 2)
{
secondsToFormat = secondsToFormat / 2;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "2";
}
if (ClicksCount == 3)
{
secondsToFormat = secondsToFormat / 3;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "3";
}
if (ClicksCount == 4)
{
secondsToFormat = secondsToFormat / 4;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "4";
}
if (ClicksCount == 5)
{
secondsToFormat = secondsToFormat / 5;
AverageMsCount.Text = secondsToFormat.ToString();
TriesCount.Text = "5";
}
if (Click`enter code here`sCount == 6)
{
ClicksCount = 1;
}
}
毫秒
属性是“秒内的毫秒”。因此,假设我们有:
secondsToFormat = endTime1.Millisecond - startTime1.Millisecond;
。。。这将给您一个-650ms的结果,因为startTime1.millished
是100,endTime1.millished
是750
理想情况下:
- 根本不要使用
:使用DateTime
,只要您想开始计时,就可以重置并启动它,然后只要您想知道经过了多少时间,就可以使用Stopwatch
属性获取appeased
。使用该TimeSpan
的TimeSpan
属性来确定经过了多少毫秒。(或者,使用totalmillizes
)秒表.ElapsedMilliseconds
- 如果您必须继续使用
DateTime
- 使用
而不是DateTime.UtcNow
,这样,如果您的用户在时区偏移更改边界(例如夏令时)上运行此操作,您不会得到奇怪的结果DateTime.Now
- 将两个值之间的差值作为
,例如TimeSpan
,然后再次使用endTime1-startTime1
属性totalmillizes
- 使用
秒表。StartNew
,它会返回一个已启动的秒表。谢谢!现在我得到了毫秒的运行时间。如何将它们转换为Int来进行除法,我想得到平均时间跨度。我现在得到的错误是:'不能应用于'TimeSpan'和'Int'类型的操作数@Rectinho:如果没有看到您的代码,我就无法判断出哪里出了错。我建议你用这个代码问一个新问题(只有相关的代码-这个问题中的大部分代码与演示问题无关)。你能给我举个例子说明如何用秒表启动计时器吗?@Rectinho:你用的是Stopwatch.StartNew
,它将返回一个已启动的秒表
。谢谢!现在我得到了毫秒的运行时间。如何将它们转换为Int来进行除法,我想得到平均时间跨度。我现在得到的错误是:'不能应用于'TimeSpan'和'Int'类型的操作数@Rectinho:如果没有看到您的代码,我就无法判断出哪里出了错。我建议您使用该代码问一个新问题(并且只有相关的代码-此问题中提供的大多数代码与演示问题无关);var elapsedMS=(endTime1-startTime1).total毫秒;