A program that reads in secounds, and prints out hours minutes
Hi, im new to Java and just testing something out. (Sorry for my bad english)
How do i make a java application where the user input secounds, and the program "transforms" these secounds into hours, minutes and secounds.
Like this:
11873
--> 3 hours, 17 minutes, 53 secounds.
Anyone got a suggestion with some simple code?(so i can understand it :P )
I cant find a way to do it.
Re: A program that reads in secounds, and prints out hours minutes
hmm.. ok. I won't give you the exact code, but i'll give you hints.
there are 60 seconds in a minute, and 3600 seconds in an hour.
So, 11873/3600 should give you how many hours there are. However, because we want the hours as an integer and not some decimal, we want to make sure that we either cast to int or use the int data type.
Code :
System.out.println("5/3 = " + 5/3); // prints out 1 because 5 and 3 are recognized as int's
System.out.println("5.0/3.0 = " 5.0/3.0); // prints out 1.666666666... because 5.0 and 3.0 are recognized as doubles
The number of hours and seconds would be the remainder of seconds left after the hours have been taken out. An easy way to get this value is by using the modulus operator. It's basically a divide operation, but instead of giving the quotient, it gives the remainder :)
Code :
System.out.println("The remainder of 100/3 is " + 100%3); // should print out 1
The left-over seconds now must be split into minutes and seconds. I'll leave that up to you, but here's a hint: copy what i said above and make a few changes ;)