Last active
March 14, 2020 15:08
-
-
Save artem-smotrakov/ec863c0b373099eb0c708dfa0627040c to your computer and use it in GitHub Desktop.
Calculating Fibonacci numbers with new switch expressions in Java 14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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