I wanted to see if I fully understood recursion so I attempted the FizzBuzz challenge and applied recursion to it.
Did I do it correctly? Is this good code? Is there a more efficient way of doing it? How can I improve?
//---------------------------------------------------------------------------------------
//[RecurFizzBuzz]
// FizzBuzz using recursion
//---------------------------------------------------------------------------------------
// Author : Jimmy
//---------------------------------------------------------------------------------------
public class RecurFizzBuzz {
// Specify a range to recurse, starts at int a, ends at int b
public static void recurse(int a, int b){
if(a <= b){
if(a % 3 == 0 && a % 5 == 0){
System.out.println("FizzBuzz");
} else if(a % 3 == 0){
System.out.println("Fizz");
} else if(a % 5 == 0){
System.out.println("Buzz");
} else {
System.out.println(a);
}
recurse(++a, b);
} else {
System.exit(0);
}
}
public static void main(String[]args) {
recurse(1, 100);
}
}