I have the following task:
Write a recursive void method that takes a single (positive) int argument n and writes the integers 1, 2, . . . , n to the screen.
What I tried so far is:
public static void write1ToNInOneLine(int n){
if (n == 0) {
return;
}
else // n is two or more digits long:
{
write1ToNInOneLine(n - 1);
System.out.print(n + ",");
}
}
but obviously I have the last redundant comma on output.
Is there a way to avoid it?