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

CodePudding user response:

reference 1st floor assky124 response:
with TimeSpan simpler

Haven't learned????

CodePudding user response:

You is normal train of thought, variable in English is also quite good,

The teacher is some simple mathematical skills, to take more time units, at the next higher level can instead of
Hours is the number of seconds in a whole day's modulo, namely, deduct the number of days zong % (3600 * 24), the rest of the number of seconds to hours/3600
Minutes is take the remainder of the number of seconds to an hour, or deduct minutes zong % 3600, the rest of the number of seconds to minutes/60;
Number of seconds is take the remainder of the number of seconds in a minute, or deduct the number of minutes, miao zong %=60, the rest of the direct is the number of seconds;

Variables do not rely on, may the compiler optimization in parallel

CodePudding user response:

The
reference 3 floor Dear200892 response:
3600 no equals 60 * 60
No big difference, you can also put [sec1] or [sec2] directly into

But why is to use the total number of seconds to deduct? Should not be ruled out the days and hours after the rest of the description division?

CodePudding user response:

reference 5 floor HZTLTGG reply:
is a normal way of thinking of you, variable in English is also quite good,

The teacher is some simple mathematical skills, to take more time units, at the next higher level can instead of
Hours is the number of seconds in a whole day's modulo, namely, deduct the number of days zong % (3600 * 24), the rest of the number of seconds to hours/3600
Minutes is take the remainder of the number of seconds to an hour, or deduct minutes zong % 3600, the rest of the number of seconds to minutes/60;
Number of seconds is take the remainder of the number of seconds in a minute, or deduct the number of minutes, miao zong %=60, the rest of the direct is the number of seconds;

Variables do not rely on, probably the compiler can optimize parallel

Thank you, I understand again??

CodePudding user response:

 
TimeSpan ts=new TimeSpan (0, 0, the number of seconds);
String STR=ts. Days + "day" + ts. Hours + "hour" + ts. Minutes + "Minutes" + ts. Seconds + "Seconds".

CodePudding user response:

refer to the eighth floor desperaso response:
 
TimeSpan ts=new TimeSpan (0, 0, the number of seconds);
String STR=ts. Days + "day" + ts. Hours + "hour" + ts. Minutes + "Minutes" + ts. Seconds + "Seconds".

Thank you, I study the

CodePudding user response:

You this is not the same as the code is also not ah, you just define the intermediate variable, merge the don't you like the same below
  •  Tags:  
  • C#
  • Related