Skip to content

Instantly share code, notes, and snippets.

@raimonizard
Last active April 25, 2023 11:53
Show Gist options
  • Save raimonizard/98cb34e63e2006c52612c8528d882689 to your computer and use it in GitHub Desktop.
Save raimonizard/98cb34e63e2006c52612c8528d882689 to your computer and use it in GitHub Desktop.
Java: Codi d'exemple per iterar sobre els valors d'un Enum
public enum Genere {
    M, F, U;
}

public class Main {
    public static void main(String[] args) {
        crearTexa();
    }
    
  public static void crearTexa(){
      Scanner llegir = new Scanner(System.in);
      Genere[] g = Genere.values();

      for (int i = 0; i < g.length; i++) {
          System.out.println("" + (i+1) + "->" + g[i]);
      }
      int x = llegir.nextInt();
      Genere genereTriat = g[x-1];
      System.out.println(genereTriat);
  }
}

Credits: https://www.tutorialspoint.com/how-to-iterate-the-values-in-an-enum-in-java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment