(This Answer is largely for the peanut gallery ...)
Here's my take on the "best" recursive solution to the problem as stated:
public String recur ( int x )
{
if ( x >= 1 && x < 10 ) {
return "A";
} else if ( x >= 10 && x < 100 ) {
return "B" + recur( x );
} else if ( x >= 100 && x < 1000 ) {
return "C" + recur( x );
} else {
throw new IllegalArgumentException();
}
}
System.err.println(recur(123));
or a bit more generally:
public String recur ( int x)
return recur0( x, 0 );
}
public String recur0 ( int x, int i )
{
if ( x <= 0 ) {
return "";
else {
return recur0( x % 10, i + 1 ) + ((char) ('A' + i));
}
}
System.err.println(recur(123));
As you can see, no StringBuilder is needed. At best, a StringBuilder is a micro-optimization. It does not make the code any safer, or any more readable. To illustrate, here's the first version redone using a StringBuilder
public void recur ( int x, StringBuilder sb )
{
if ( x >= 1 && x < 10 ) {
sb.append("A");
} else if ( x >= 10 && x < 100 ) {
sb.append("B");
recur( x, sb );
} else if ( x >= 100 && x < 1000 ) {
sb.append("C");
recur( x, sb );
}
}
StringBuilder sb = new StringBuilder();
recur(123, sb);
System.err.println(sb.toString());
But the simplest solution for the problem as originally stated is this:
public String nonRecur(int x) {
if ( x >= 1 && x < 10 ) {
return "A";
} else if ( x >= 10 && x < 100 ) {
return "BA";
} else if ( x >= 100 && x < 1000 ) {
return "CBA";
} else {
throw new IllegalArgumentException();
}
}
System.err.println(nonRecur(123));
Note that there is considerable ambiguity in the way that the problem was stated, and @ÓscarLópez's solution is based on a different interpretation of the problem than mine. He is mapping the digits of the numbers to letters, rather than using the letters to signify magnitude.
In fact, the OP's actual problem is neither of these interpretations, and I don't think that any of the Answers will actually help him ... even though they do answer his Question.