List<String> result = new ArrayList<>();
for(int i=0; i<=5; ++i) {
StringBuilder sb = new StringBuilder();
for(int j=i; j>=0; j--) {
sb.append((int)Math.pow(2, j));
}
result.add(sb.toString());
}
// You need to find out the longgest string for the padding left calculation
int length = result.get(result.size() - 1).length();
result.forEach((str -> System.out.println(padLeft(str, length))));
Добавить метод до для заполнения:
public String padLeft(String inputString, int length) {
if (inputString.length() >= length) {
return inputString;
}
StringBuilder sb = new StringBuilder();
while (sb.length() < length - inputString.length()) {
sb.append(' ');
}
sb.append(inputString);
return sb.toString();
}