Untitled

 avatar
unknown
java
3 years ago
2.7 kB
6
Indexable
import java.util.Scanner;

public class Main {

    // Метод для печати символа N раз (в 1 строку)
    public static void drawCharacter(char character, int times) {
        for (int i = 1; i <= times; i++) {
            System.out.print(character);
        }
    }

    // Метод для отрисовки горизонтальной границы рисунка
    public static void drawHorizontalBound(int effectiveWidth, boolean breakLine) {
        drawCharacter(' ', 1);
        drawCharacter('-', effectiveWidth);
        drawCharacter(' ', 1);
        if (breakLine) {
            System.out.println();
        }
    }

    // Метод для вычисления ЭФФЕКТИВНОЙ высоты рисунка (то есть не включающей
    // верхнюю и нижнюю границы)
    public static int calculateHeight(int effectiveWidth) {
        int height = 1;
        int decreaseNumber = 0;
        for (int i = effectiveWidth - 1; i > 0; i -= decreaseNumber) {
            if (i - decreaseNumber <= 0) {
                return height;
            } else {
                height += 2;
            }
            decreaseNumber++;
        }
        return height;
    }

    public static void drawLine(int effectiveWidth, int startIndex, int count) {
        drawCharacter('|', 1);
        drawCharacter(' ', startIndex);
        // Обрубание символов если сумма отступа и их количества больше допустимой ширины
        if (count + startIndex > effectiveWidth) {
            while (count + startIndex > effectiveWidth) {
                count--;
            }
        }
        drawCharacter('!', count);
        drawCharacter(' ', effectiveWidth - startIndex - count);
        drawCharacter('|', 1);
        System.out.println();
    }

    public static void drawPicture(int srcWidth) {
        int effectiveWidth = srcWidth - 2;
        int height = calculateHeight(effectiveWidth);
        drawHorizontalBound(effectiveWidth, true);
        int i;
        int currOffset = 0;
        for (i = 1; i <= (height-1)/2; i++) {
            drawLine(effectiveWidth, currOffset, i);
            currOffset += i;
        }
        for (i = (height-1)/2; i >= 0; i--){
            drawLine(effectiveWidth, currOffset, i + 1);
            currOffset -= i;
        }
        drawHorizontalBound(effectiveWidth, true);
    }

    public static void main(String[] args) {
        System.out.print("Введите w: ");
        Scanner scanner = new Scanner(System.in);
        int w = scanner.nextInt();
        System.out.println();
        drawPicture(w);
    }
}
Editor is loading...