以 MM:SS 格式和 return 作为字符串添加两次以达到最大精度
Add two times in MM:SS format and return as string to maximum precision
我想编写一个添加 2 个字符串的函数,一个格式为 MM:ss,另一个以秒为单位,然后才进行添加(处理额外的秒数)returns具有最大精度的相同格式。
// 例如:“1:30”+“65”=“2:35”
我已经从我这边试过了,但我认为它可以变得更通用,请看代码
public static string maxLevel(string s1, string s2)
{
string[] parts1 = null;
string[] parts2 = null;
if (s1.Contains(":"))
{
parts1 = s1.Split(':');
}
if (s2.Contains(":"))
{
parts2 = s2.Split(':');
}
int minutes1 = 0;
int seconds1 = 0;
int minutes2;
int seconds2 = 0;
int newSeconds = 0;
int newMinutes = 0;
int carry = 0;
if (parts1 != null && parts1.Length > 1)
{
minutes1 = Convert.ToInt32(parts1[0]);
// int HoursInminutes = (12 + (minutes % 60)) * 60;
seconds1 = Convert.ToInt32(parts1[1]);
}
else
{
seconds1 = Convert.ToInt32(s1);
int minutes = seconds1 / 60;
if (minutes >= 1)
{
carry = seconds1 - 60;
//newSeconds = seconds2 + carry;
}
newMinutes = minutes;
newSeconds = carry;
}
if (parts2 != null && parts2.Length > 1)
{
minutes2 = Convert.ToInt32(parts2[0]);
seconds2 = Convert.ToInt32(parts2[1]);
//newMinutes = minutes2;
newMinutes += minutes2;
newSeconds += seconds2;
}
else
{
seconds2 = Convert.ToInt32(s2);
int minutes = seconds2/60;
if(minutes >= 1)
{
carry = seconds2 - 60;
newSeconds = seconds1 + carry;
newMinutes = minutes1 + minutes;
}
else
{
carry = seconds2 - seconds1;
newSeconds = carry;
newMinutes = 1 + minutes1;
}
}
return newMinutes + ":" + newSeconds;
}
当我将 s1 更改为 55 秒并将 s2 更改为 1:30 时,此代码不起作用。
我认为它需要一些修改,有人可以帮助我或告诉我 C#
中的正确方法吗
既然要让用户指定超过59秒(and/or分钟),我觉得TimeSpan.TryParseExact
不行,因为你不能超过59
秒或分钟到它。这些字段必须是 0-59
.
但是,您可以编写自己的自定义解析器,在冒号字符 (:
) 上拆分输入字符串,然后使用 int.TryParse
尝试将结果部分解析为整数,然后您可以使用 TimeSpan.FromSeconds
传递任意秒数以创建新的 TimeSpan
对象,并且您可以使用 .Add
方法添加从分钟部分创建的另一个时间跨度(如果已指定) .
首先我们可以编写一个方法,它将 return 一个 TimeSpan
从格式为 "[integer]" 或 "[integer]:[integer]":
public static TimeSpan CustomParse(string input)
{
// Split the string on the ':' character
var parts = input?.Split(':');
// Make sure we have something to work with
if (parts == null || parts.Length == 0)
throw new FormatException("input format must be \"%m:%s\" or \"%s\"");
int seconds;
// Only a single number represents seconds
if (parts.Length == 1)
{
if (int.TryParse(parts[0], out seconds))
{
return TimeSpan.FromSeconds(seconds);
}
}
// Otherwise the first number is minutes and the second one is seconds
else
{
int minutes;
if (int.TryParse(parts[0], out minutes) &&
int.TryParse(parts[1], out seconds))
{
return TimeSpan.FromSeconds(seconds).Add(TimeSpan.FromMinutes(minutes));
}
}
// If we haven't returned anything yet, there was an error in the format
throw new FormatException("input format must be \"%m:%s\" or \"%s\"");
}
然后我们可以编写另一个函数,它接受两个字符串,使用我们上面的方法将它们转换为时间跨度,returns 将它们作为字符串加在一起的结果:
public static string Add(string s1, string s2)
{
return CustomParse(s1).Add(CustomParse(s2)).ToString("%m\:%s");
}
现在我们可以使用您的示例字符串对此进行测试:
private static void Main()
{
string first = "1:30";
string second = "65";
string result = Add(first, second);
Console.WriteLine($"{first} + {second} = {result}");
GetKeyFromUser("\nDone! Press any key to exit...");
}
输出
我想编写一个添加 2 个字符串的函数,一个格式为 MM:ss,另一个以秒为单位,然后才进行添加(处理额外的秒数)returns具有最大精度的相同格式。 // 例如:“1:30”+“65”=“2:35”
我已经从我这边试过了,但我认为它可以变得更通用,请看代码
public static string maxLevel(string s1, string s2)
{
string[] parts1 = null;
string[] parts2 = null;
if (s1.Contains(":"))
{
parts1 = s1.Split(':');
}
if (s2.Contains(":"))
{
parts2 = s2.Split(':');
}
int minutes1 = 0;
int seconds1 = 0;
int minutes2;
int seconds2 = 0;
int newSeconds = 0;
int newMinutes = 0;
int carry = 0;
if (parts1 != null && parts1.Length > 1)
{
minutes1 = Convert.ToInt32(parts1[0]);
// int HoursInminutes = (12 + (minutes % 60)) * 60;
seconds1 = Convert.ToInt32(parts1[1]);
}
else
{
seconds1 = Convert.ToInt32(s1);
int minutes = seconds1 / 60;
if (minutes >= 1)
{
carry = seconds1 - 60;
//newSeconds = seconds2 + carry;
}
newMinutes = minutes;
newSeconds = carry;
}
if (parts2 != null && parts2.Length > 1)
{
minutes2 = Convert.ToInt32(parts2[0]);
seconds2 = Convert.ToInt32(parts2[1]);
//newMinutes = minutes2;
newMinutes += minutes2;
newSeconds += seconds2;
}
else
{
seconds2 = Convert.ToInt32(s2);
int minutes = seconds2/60;
if(minutes >= 1)
{
carry = seconds2 - 60;
newSeconds = seconds1 + carry;
newMinutes = minutes1 + minutes;
}
else
{
carry = seconds2 - seconds1;
newSeconds = carry;
newMinutes = 1 + minutes1;
}
}
return newMinutes + ":" + newSeconds;
}
当我将 s1 更改为 55 秒并将 s2 更改为 1:30 时,此代码不起作用。 我认为它需要一些修改,有人可以帮助我或告诉我 C#
中的正确方法吗既然要让用户指定超过59秒(and/or分钟),我觉得TimeSpan.TryParseExact
不行,因为你不能超过59
秒或分钟到它。这些字段必须是 0-59
.
但是,您可以编写自己的自定义解析器,在冒号字符 (:
) 上拆分输入字符串,然后使用 int.TryParse
尝试将结果部分解析为整数,然后您可以使用 TimeSpan.FromSeconds
传递任意秒数以创建新的 TimeSpan
对象,并且您可以使用 .Add
方法添加从分钟部分创建的另一个时间跨度(如果已指定) .
首先我们可以编写一个方法,它将 return 一个 TimeSpan
从格式为 "[integer]" 或 "[integer]:[integer]":
public static TimeSpan CustomParse(string input)
{
// Split the string on the ':' character
var parts = input?.Split(':');
// Make sure we have something to work with
if (parts == null || parts.Length == 0)
throw new FormatException("input format must be \"%m:%s\" or \"%s\"");
int seconds;
// Only a single number represents seconds
if (parts.Length == 1)
{
if (int.TryParse(parts[0], out seconds))
{
return TimeSpan.FromSeconds(seconds);
}
}
// Otherwise the first number is minutes and the second one is seconds
else
{
int minutes;
if (int.TryParse(parts[0], out minutes) &&
int.TryParse(parts[1], out seconds))
{
return TimeSpan.FromSeconds(seconds).Add(TimeSpan.FromMinutes(minutes));
}
}
// If we haven't returned anything yet, there was an error in the format
throw new FormatException("input format must be \"%m:%s\" or \"%s\"");
}
然后我们可以编写另一个函数,它接受两个字符串,使用我们上面的方法将它们转换为时间跨度,returns 将它们作为字符串加在一起的结果:
public static string Add(string s1, string s2)
{
return CustomParse(s1).Add(CustomParse(s2)).ToString("%m\:%s");
}
现在我们可以使用您的示例字符串对此进行测试:
private static void Main()
{
string first = "1:30";
string second = "65";
string result = Add(first, second);
Console.WriteLine($"{first} + {second} = {result}");
GetKeyFromUser("\nDone! Press any key to exit...");
}
输出