如何将用户输入格式化为时间(How to format user input into time)

编程入门 行业动态 更新时间:2024-10-18 18:26:41
如何将用户输入格式化为时间(How to format user input into time)

我在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);

更多推荐

本文发布于:2023-07-16 12:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   格式   时间   用户   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!