Sleep: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Ada}}: - cleaned up a paste error.)
m (Fixed up task description a bit.)
Line 1: Line 1:
{{task}}
{{task}}
Write a program that does the following in this order:
Write a program that does the following in this order:
* Input an amount of time to sleep in whatever units are most natural
* Input an amount of time to sleep in whatever units are most natural for your language (milliseconds, seconds, ticks, etc.). This unit should be noted somewhere.
for your language (milliseconds, seconds, ticks, etc.).
* Print "Sleeping..."
* Print "Sleeping..."
* Sleep the main thread for the given amount of time.
* Sleep the main thread for the given amount of time.

Revision as of 02:34, 11 December 2007

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.). This unit should be noted somewhere.
  • Print "Sleeping..."
  • Sleep the main thread for the given amount of time.
  • Print "Awake!"
  • End.

Ada

The Ada delay statement takes an argument of type Duration, which is a real number counting the number of seconds to delay. Thus, 2.0 will delay 2.0 seconds, while 0.001 will delay 0.001 seconds.

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;

procedure Sleep is
   In_Val : Float;
begin
   Get(In_Val);
   Put_Line("Sleeping...");
   delay Duration(In_Val);
   Put_Line("Awake!");
end Sleep;

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!");
	}
}