Skip to main content
9 of 11
added 88 characters in body
Jens
  • 297
  • 3
  • 13

If you can use commons-lang3:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Need to add the commons-lang3 library to your build/classpath.

Jens
  • 297
  • 3
  • 13