Skip to content

Instantly share code, notes, and snippets.

@artem-smotrakov
Last active March 14, 2020 15:08
Show Gist options
  • Save artem-smotrakov/ec863c0b373099eb0c708dfa0627040c to your computer and use it in GitHub Desktop.
Save artem-smotrakov/ec863c0b373099eb0c708dfa0627040c to your computer and use it in GitHub Desktop.
Calculating Fibonacci numbers with new switch expressions in Java 14
public class Fibonacci {
public static void main(String[] args) {
for (String arg : args) {
int n = Integer.valueOf(arg);
System.out.printf("Fibonacci(%d) = %d%n", n, fibonacci(n));
}
}
static int fibonacci(int n) {
return switch (n) {
case 0 -> 0;
case 1 -> 1;
default -> fibonacci(n - 1) + fibonacci(n - 2);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment