Sunday, July 26, 2009

C# Main Method help?

Can anyone help?? I am having difficulty.





A C# solution with a Main ( ) method that holds an integer variable named seconds to which you will assign a value. Create a method to which you pass this value. The method displays the seconds in minutes and seconds. As an example 66 seconds is 1 minute and 6 seconds.

C# Main Method help?
is this for homework???








class TestClass


{


static void Main(string[] args)


{


int sec;


sec = args[0];


displayTime(sec);


}


void displayTime(int sec)


{


System.Console.WriteLine( sec / 60 + " " + sec - ((sec/60) * 60));


}


}
Reply:Are you having trouble calculating the number of minutes/seconds given the number of seconds?





You can calculate that with this code fragment:





int seconds = 66;


int minutes = seconds / 60;


int seconds = seconds - minutes*60





minutes now holds 1 and seconds holds 6
Reply:I'd follow SlaveMaker's suggestion for a class format (although they didn't ask you to accept command line input, they asked you to "assign a value" which may imply a mere "seconds = 100;")





You may want to use the framework's System.TimeSpan for your calculations (even if they're straightforward, it removes ambiguity and rounding errors). Basically





TimeSpan ts = new TimeSpan(0, 0, x); // where x is the number of seconds


Console.WriteLine( String.Format("{0} minute{1} and {2} second{3}", ts.Minutes, ts.Minutes == 1 ? "" : "s", ts.Seconds, ts.Seconds == 1 ? "" : "s"));





This is merely a suggestion. They may indeed want you to do the math or they may just want you to use the framework. *shrug*
Reply:One second...


No comments:

Post a Comment