Sleep

From Rosetta Code
Revision as of 00:49, 11 December 2007 by rosettacode>Mwn3d (Created page with Java example.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Sleep
You are encouraged to solve this task according to the task description, using any language you may know.

Write a program that does the following in this order:

  • Input an amount of time to sleep in whatever units are most natural

for your language (milliseconds, seconds, ticks, etc.).

  • Print "Sleeping..."
  • Sleep the main thread for the given amount of time.
  • Print "Awake!"
  • End.

Java

import java.util.Scanner;

public class Sleep{
	public static void main(String[] args) throws InterruptedException{
		Scanner input = new Scanner(System.in);
		int ms = input.nextInt(); //Java's sleep method accepts milliseconds
		System.out.println("Sleeping...");
		Thread.sleep(ms);
		System.out.println("Awake!");
	}
}