Is anyway to introduce time in loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is anyway to introduce time in loop?

I want to introduce time in my loop, like for(i=1sec;i<=10sec;i++) anyway to implement this on a program. I want to count time and the total number of times. Any possible way. like say "Hello" in 5 times in an interval 2 sec. I know the above won't work tho😅. Any help

7th Nov 2017, 8:08 AM
speCtre
speCtre - avatar
5 Answers
+ 3
try this: the loop will print the current time once every 5 seconds: try { while (true) { System.out.println(new Date()); Thread.sleep(5 * 1000); } } catch (InterruptedException e) { e.printStackTrace(); }
7th Nov 2017, 8:25 AM
Austris Otersons-Andersons
Austris Otersons-Andersons - avatar
+ 1
what you're looking for is a delay ( threads sleep ) function. # talking about c++ Windows has "Sleep(miliseconds)" Unix has "usleep(microseconds)" https://code.sololearn.com/cwYuFn2WFeFH/?ref=app you shouldn't run it from sololearn application.
7th Nov 2017, 8:20 AM
Cain Eviatar
Cain Eviatar - avatar
0
error @Cain
7th Nov 2017, 8:28 AM
speCtre
speCtre - avatar
0
Can you help me with a program to type "Hello" 10 times in a interval of 5 sec each
7th Nov 2017, 8:35 AM
speCtre
speCtre - avatar
0
@Sabareesh A P I updated the function.
7th Nov 2017, 8:36 AM
Cain Eviatar
Cain Eviatar - avatar