2015-10-02 48 views
0

我正在编写一个程序,你需要在几分钟内输入你已经骑自行车多久,然后显示你在几小时内骑了多长时间hh:mm 因此对于例如,如果用户在88分钟写的,应该显示1:28C#如何显示用户输入的整数作为时间

我找了好几个小时,现在并不能找到如何做到这一点,也许并不能帮助我总新手编码:)

//input data 
      Console.Write("Number of Minutes spent Cycling..."); 
      int cycling = int.Parse(Console.ReadLine()); 
//perform calculations 

      TimeSpan cTime = TimeSpan.FromHours(cycling); 
      string fromTimeString = cTime.ToString("hh':'mm"); 
//output results 
      Console.WriteLine("cycling {0}", cTime); 

      Console.ReadLine(); 
     } 

这只是我尝试过的很多方法之一,谢谢你的帮助:)

+1

难道你不想写“FromMinutes”吗? –

+1

谢谢,我刚刚看到这个我的自我,这样一个愚蠢的错误 – Nathanhall

回答

1

只是需要一些小调整:

//input data 
     Console.Write("Number of Minutes spent Cycling..."); 
     int cycling = int.Parse(Console.ReadLine()); 
//perform calculations 

     TimeSpan cTime = TimeSpan.FromMinutes(cycling); 
     string fromTimeString = cTime.ToString("h\\:mm"); 
//output results 
     Console.WriteLine("cycling {0}", fromTimeString); 

     Console.ReadLine(); 

所以基本上改变fromHoursfromMinutes根据本文档适合您的输入和修复您的格式字符串:https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx 我改变hh:mmh:mm,因为你可能不要我们不想在几个小时内领先零。

当然还有打印输出的小语法缺陷,您使用cTime而不是您准备好的字符串。

+0

谢谢你的回答,我没有注意到语法错误,再次感谢:) – Nathanhall

+0

我也试过有(“h:mm”),我得到了一个崩溃的错误,然后我把它改为(h':'mm) ) – Nathanhall

+0

我的不好...对于':'你需要一个转义字符......所以它应该是'“h \\:mm”' – SkryptX

1

您正在提示用户以分钟输入时间,之后您尝试解析的时间为小时。你应该使用TimeSpan.FromMinutes(循环)。

+1

omg我刚刚看到这个我的自我,愚蠢的错误谢谢你 – Nathanhall

+0

你知道我是否可以删除秒部分? – Nathanhall

相关问题