Home > Net > Programming implementation, user input how many seconds, then the program is converted to a few days
Programming implementation, user input how many seconds, then the program is converted to a few days
Time:09-17
//practice 5: programming, how many seconds, the user input and process into a few days a few hours, a few minutes to a few seconds, //my code is: Console. WriteLine (" please enter the total number of seconds: "); String STR=Console. ReadLine (); Int the SEC=the Convert. ToInt32 (STR); Int the SEC/day=(60 * 60 * 24); Int sec1=the SEC % (60 * 60 * 24); Int hour=sec1/3600; Int sec2=sec1 % (60 * 60); Int min=sec2/60; Int sec3=sec2%60; Console. WriteLine (" {0} seconds into {2} {1} days hours {3} {4} seconds, ", the SEC, day, hour, min, sec3);
//the teacher of the code is: Console. WriteLine (" please enter the total number of seconds: "); Int zong=the Convert. ToInt32 (Console. ReadLine ()); Int tian=zong/(3600 * 24); Int shi zong=% (3600 * 24)/3600; Int fen zong %=3600/60; Int miao zong=% 60; Console. WriteLine (" {0} seconds into {2} {1} days hours {3} {4} seconds, ", zong, tian, shi, fen, miao); Console.ReadKey();
These two code results are the same, but my code clear complex, My train of thought is: the number of days it should be the total number of seconds/3600/24, the number of hours and days should be the number of seconds remaining hours/3600 minutes should be the rest of the number of seconds/60 is the number of seconds left to the number of seconds, But teacher code to the number of minutes are directly used for more than 3600 and then divided by the total number of minutes, 60 seconds is directly in the total number of minutes for more than 60 Why is this?
CodePudding user response:
It's easier to use TimeSpan
CodePudding user response:
Data type is int, "/" symbol is integer arithmetic, rather than the rounded, If the data type is a Double, is the division of "/"
CodePudding user response:
3600 no equals 60 * 60 No big difference, you can also put [sec1] or [sec2] written directly