如何确定较小的时间C#

本文关键字:时间 何确定 | 更新日期: 2023-09-27 18:27:31

我需要一种方法来存储最小的时间来替换任何现有的,但目前我尝试的[下面]不起作用,有时可能会说2:38.4小于2:20.1。

在文本文件中

88:88:8

在表单3文本框中

timerMin
timerSec
timerMil

写入正确的路径。

                using (TextReader reader = File.OpenText(pathPlayer + player[id].name + "''time.txt"))
                {
                    string z = reader.ReadLine();
                    string[] zsplit = z.Split(':');
                    reader.Close();
                    fileMin = Convert.ToInt32(timerMinute.Text);
                    recMin = Convert.ToInt32(zsplit[0]);
                    if (fileMin < recMin)
                    {
                        File.WriteAllText(pathPlayer + player[id].name + "''time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                        newPersonalRecord = true;
                    }
                    else
                    {
                        fileSec = Convert.ToInt32(timerSecond.Text);
                        recSec = Convert.ToInt32(zsplit[1]);
                        if (fileSec < recSec)
                        {
                            File.WriteAllText(pathPlayer + player[id].name + "''time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                            newPersonalRecord = true;
                        }
                        else
                        {
                            fileMil = Convert.ToInt32(timerMili.Text);
                            recMil = Convert.ToInt32(zsplit[1]);
                            if (fileMil < recMil)
                            {
                                File.WriteAllText(pathPlayer + player[id].name + "''time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                                newPersonalRecord = true;
                            }
                            else
                            {
                            }
                        }
                    }
                }

我已经为此工作了很长一段时间,我看不出我哪里出了问题,如果能提供帮助,那将是非常棒的。

感谢

如何确定较小的时间C#

当您应该比较TimeSpans 时,您正在比较文本框

如果文件中的字符串没有超过一天中的时间(最多为"23:59:59")然后您可以使用字符串通过执行TimeSpan.Parse("18:44:08");来创建TimeSpans,并像一样进行比较

                fileMin = new TimeSpan(0, 0, int.Parse(timerMin), int.Parse(timerSec), int.Parse(timerMil));
                recTimeSpan = TimeSpan.Parse(z);
                if(fileMin > recTimeSpan)
                {// Your code}
                else
                {// Your code}

你总是可以做

                recTimeSpan = new TimeSpan(0, 0, int.Parse(zsplit[0]), int.Parse(zsplit[1]), int.Parse(zsplit[2]));

这更像是一个设计技巧,但我强烈建议您使用TimeSpan类来处理类似的问题,而不是整数;这将使您的代码更易于编写和阅读。您可以根据正在检索的时间数据构造一个新的TimeSpan,然后只需使用一个比较运算符来确定它是否大于、小于或等于现有记录,而不是一个没有分钟的秒和毫秒的比较运算符。

如果您使用DateTime来跟踪开始和结束时间,DateTime也可以工作(只需使用减法运算符,就可以很容易地将DateTime作为TimeSpan来查找差异)

例如:

DateTime StartTime
DateTime EndTime 
TimeSpan difference = EndTime = StartTime 

您的代码有很多重复,您应该设法消除这些重复。您也可以使用TimeSpan来存储值并进行比较。

试试这样的东西:

string filePath = pathPlayer + player[id].name + "''time.txt";
string z = null;
using (TextReader reader = File.OpenText(filePath))
{
    z = reader.ReadLine();
}
string[] zsplit = z.Split(':');
//Create a timespan from the values read from the .txt file
TimeSpan fileTime = new TimeSpan(0, 0, int.parse(zsplit[0]), int.parse(zsplit[1]), int.parse(zsplit[2]));
//Create a timespan from the values stored in the Textbox controls
TimeSpan inputTime = new TimeSpan(0, 0, int.parse(timerMinute.Text), int.parse(timerSecond.Text), int.parse(timerMili.Text));
//Check if the new TextBox time is faster than the time stored in the file
if(inputTime < fileTime)
{
    //new faster time, so update the file
    File.WriteAllText(filePath, inputTime.ToString("mm:ss:f"));
    newPersonalRecord = true;
}

需要注意的几点:

  • 输入上没有验证,因此无效数据将使应用程序崩溃(您应该添加验证,请参阅:int.TryParse)。您还需要验证z不是null,并且zsplitLength至少为3
  • 您对fileMinrecMin的命名与您的数据源不匹配,为了更清楚起见,我将它们重命名为fileTimeinputTime
  • 本例检查inputTime是否小于fileTime,如果您希望使用其他方式,则只需在if语句中切换它们
  • 在本例中使用TimeSpan假设微小分量永远不能大于59