CommencerCommencer gratuitement

Navigating with Enum

Enums are great for representing real-world directions clearly in code. Imagine you're creating a navigation feature for an application that provides instructions to users.

Cet exercice fait partie du cours

Input/Output and Streams in Java

Afficher le cours

Instructions

  • Define an enum named Direction containing constants: NORTH, EAST, SOUTH, and WEST.
  • Create a method with Direction parameter.
  • Print the EAST direction using printDirection().

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

public class EnumDemo {
	// Define enum
    ____ Direction { 
        NORTH, EAST, SOUTH, WEST
    }

	// Method with enum parameter
    static void printDirection(____ direction) { 
        System.out.println("Direction: " + direction);
    }

	// Call method with EAST
    public static void main(String[] args) {
        printDirection(____);
    }
}
Modifier et exécuter le code