The problem is that the stars don't form a Christmas tree, they just form a right triangle. How to solve it? https://i.imgur.com/vKpxRhP.png <--- This is what it looks like. https://i.imgur.com/eyuccNn.png <--- This is what it should look like.
public static void main(String[] args) throws FileNotFoundException {
File file = new File("In0103.txt");
Scanner in = new Scanner(file);
int maxWidth = in.nextInt();
int initialWidth = 1;
int initialLevel = 1;
int maxLevel = (maxWidth + 1) / 2;
int freeSpace = maxLevel - 1;
printChristmasTree(initialWidth, initialLevel, maxLevel, freeSpace, null);
}
static void printChristmasTree(int width, int level, int maxLevel, int freeSpace, PrintWriter save) throws FileNotFoundException {
if (save == null) {
save = new PrintWriter("Out0103.txt");
}
if (level < maxLevel) {
save.println(Stream.generate(() -> " ").limit(freeSpace).collect(Collectors.joining()));
save.println(Stream.generate(() -> "*").limit(width).collect(Collectors.joining()));
save.println();
printChristmasTree(width = width + 2, ++level, maxLevel, --freeSpace, save);
} else {
save.println(Stream.generate(() -> " ").limit(freeSpace).collect(Collectors.joining()));
save.println(Stream.generate(() -> "*").limit(width).collect(Collectors.joining()));
save.close();
}
}