Home > Mobile >  Incorrect display of the Christmas tree
Incorrect display of the Christmas tree

Time:11-10

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();
    }

}

CodePudding user response:

You almost got it. But you are printing the freeSpace with save.println() but you do not need a new line, just save.print(). Additionally, there is one extra save.println() that is also not needed:

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(maxWidth, initialWidth, initialLevel, maxLevel, freeSpace, null);
}

static void printChristmasTree(int maxWidth, int width, int level, int maxLevel, int freeSpace, PrintWriter save) throws FileNotFoundException {
    if (save == null) {
        save = new PrintWriter("Out0103.txt");
        save.println("n ="   maxWidth   "");
    }

    if (level < maxLevel) {
        save.print(Stream.generate(() -> " ").limit(freeSpace).collect(Collectors.joining()));
        save.println(Stream.generate(() -> "*").limit(width).collect(Collectors.joining()));
        printChristmasTree(maxWidth, width   2,   level, maxLevel, --freeSpace, save);
    } else {
        save.print(Stream.generate(() -> " ").limit(freeSpace).collect(Collectors.joining()));
        save.println(Stream.generate(() -> "*").limit(width).collect(Collectors.joining()));
        save.close();
    }
}

And maybe you need to change the line from int freeSpace = maxLevel - 1; to int freeSpace = maxLevel;, given that it seems there is a free space in the last row of your tree.

  • Related