问题描述
我已经陷入了程序中,我需要计算金/分钟,但是我的数学公式不会做欲望的事情.当我将小时输入浮标(大约1.2小时)时,转换为72分钟,而不是我需要的80分钟. 你能帮我么 ?我在下面的评论中标记了问题所在. 这是我的代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace YourGold { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to YourGold App! \n------------------------"); Console.WriteLine("Inesrt your gold: "); int gold = int.Parse(Console.ReadLine()); Console.WriteLine("Your gold is : " + gold); Console.WriteLine("Inesrt your time(In Hours) played: "); float hours = float.Parse(Console.ReadLine()); int minutes = 60; float time = (float)hours * minutes; // Here the calculation are wrong... Console.WriteLine("Your total time playd is : " + time + " minutes"); float goldMin = gold / time; Console.WriteLine("Your gold per minute is : " + goldMin); Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it."); Console.ReadLine(); } } }
非常感谢.
p.s它与此问题有关:仅允许插入数字并将小时转换为几分钟来计算金/最小值 - 更新,我将其更新与此相同,但我认为我应该像现在这样做一个新问题(我' m仍在学习如何继续使用此平台:))
推荐答案
var hours = 1.2; var minutes = ((int)hours) * 60 + (hours%1)*100;
和旁注:输入时间的这种方式不是一个好的.这会令人困惑,我想人们通常会真正进入1:20而不是1.2,这会破坏您的应用程序.如果没有,他们可能会输入1.5思考90分钟.我知道我会那样做.
其他推荐答案
使用内置的 TimeSpan TimeSpan :
TimeSpan time = TimeSpan.FromHours(1.2); double minutes = time.TotalMinutes;
TimeSpan.FromHours 方法方法返回一个时间pan代表指定数量的小时数,其中规范准确至最近的毫秒.
您也可以做:
// string timeAsString = "1:20"; TimeSpan time; if (TimeSpan.TryParse(timeAsString, CultureInfo.InvariantCulture, out time)) { double minutes = time.TotalMinutes; //... continue } else { // Ask user to input time in correct format }
或:
var time = new TimeSpan(0, 1, 20, 0); double minutes = time.TotalMinutes;
其他推荐答案
如果您真的希望您的程序按照您的意愿行事.
time = (int)hours * 60 + (hours%1)*100
问题描述
I've got stuck in my program, i need to calculate the gold/minute but my math formula won't do the desire thing. As I input the hours into a float(something like 1.2 hours) the transformation will be 72 min instead of 80 as I need. Can you please help me ? I marked in comment below where the problem is. And here is my code :
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace YourGold { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to YourGold App! \n------------------------"); Console.WriteLine("Inesrt your gold: "); int gold = int.Parse(Console.ReadLine()); Console.WriteLine("Your gold is : " + gold); Console.WriteLine("Inesrt your time(In Hours) played: "); float hours = float.Parse(Console.ReadLine()); int minutes = 60; float time = (float)hours * minutes; // Here the calculation are wrong... Console.WriteLine("Your total time playd is : " + time + " minutes"); float goldMin = gold / time; Console.WriteLine("Your gold per minute is : " + goldMin); Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it."); Console.ReadLine(); } } }
Thanks a lot.
P.S it's related to this question:Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED , I update it same as this but i think i should have done a new question as i did now(I'm still learning how to go on with this platform:) )
推荐答案
var hours = 1.2; var minutes = ((int)hours) * 60 + (hours%1)*100;
And a side note: such way of inputting time is IMO not a good one. It'll be confusing and I guess that more often than not people will be actually entering 1:20 instead of 1.2, which'll break your application. And if not, they might be entering 1.5 thinking of 90 minutes. I know I would have done it like that.
其他推荐答案
Use the built-in TimeSpan:
TimeSpan time = TimeSpan.FromHours(1.2); double minutes = time.TotalMinutes;
TimeSpan.FromHours Method Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
You can also do:
// string timeAsString = "1:20"; TimeSpan time; if (TimeSpan.TryParse(timeAsString, CultureInfo.InvariantCulture, out time)) { double minutes = time.TotalMinutes; //... continue } else { // Ask user to input time in correct format }
Or:
var time = new TimeSpan(0, 1, 20, 0); double minutes = time.TotalMinutes;
其他推荐答案
If you really want your program to behave as you want do this.
time = (int)hours * 60 + (hours%1)*100