我在C#中编写一个简单的类,它所做的就是将三个浮点变量打印到DOS控制台(小时,分钟,秒)。 输出是这样的:小时= 3,分钟= 15,秒= 0.但我想知道如何格式化它以显示3:15:00而不是小时= 3,分钟= 15,秒= 0
这是我创建的打印信息的方法:
/// <summary> /// Prints the time to the console /// </summary> public void PrintTime() { Console.WriteLine(pHour.ToString() + ":" + pMinutes.ToString() + ":" + Seconds.ToString()); }有人可以帮我弄清楚如何格式化吗? 我上网了解了DateTime,但它也需要日期,我不需要为此作业添加。 提前谢谢了!
I am writing a simple class in C# that all it does is print three float variables to a DOS console (hour, minutes, seconds). The output is something like this: Hour = 3, Minutes = 15, Seconds = 0. But I want to know how would I go about in formatting it to show 3:15:00 rather than Hour = 3, Minutes = 15, Seconds = 0
This is the method I created that prints out the info:
/// <summary> /// Prints the time to the console /// </summary> public void PrintTime() { Console.WriteLine(pHour.ToString() + ":" + pMinutes.ToString() + ":" + Seconds.ToString()); }Can someone help me figure out how to format this? I went online and found out about DateTime but it requires the date as well and I don't need to add that for this homework. Many thanks in advance!
最满意答案
您可以使用Console.WriteLine提供的字符串格式:
Console.WriteLine("{0}:{1:00}:{2:00}", pHour, pMinutes, Seconds);You can use the string format that Console.WriteLine provides:
Console.WriteLine("{0}:{1:00}:{2:00}", pHour, pMinutes, Seconds);更多推荐
发布评论