就差一点啊qwq(差个*点)why?
查看原帖
就差一点啊qwq(差个*点)why?
749028
QAQ5楼主2024/12/26 18:41
import javax.print.attribute.UnmodifiableSetException;
import java.lang.reflect.Field;

public class Main {
    public static void main(String[] args) {
        //在字符画布中绘制字符菱形
        CharCanvas charCanvas = new CharCanvas(new Size(5, 5));
        Pen pen = new Pen(charCanvas);
        pen.soak(new CharPaint('*'));
        //笔的初始位置
        Place initPos = new Place(2, 0);
        pen.click(initPos);
        Place currentPos = null;
        //第二行
        for (int count = 0; count < 3; count++) {
            currentPos = new Place(initPos.x - 1 + count, initPos.y + 1);
            pen.click(currentPos);
        }
        //第三行
        for (int drift = 0; drift < 5; drift++) {
            currentPos = new Place(0 + drift, 2);
            pen.click(currentPos);
        }
        //第四行,复制粘贴第二行来的
        for (int count = 0; count < 3; count++) {
            currentPos = new Place(initPos.x - 1 + count, initPos.y + 1 + 2);
            pen.click(currentPos);
        }
        //最后一笔点,完成!
        pen.click(new Place(initPos.x, 5 - 1));
        //忘记压缩了,输出要的是压缩的结果
        charCanvas.compress();
        //别忘了展示
        System.out.println(charCanvas);
    }
}

//笔类型
class Pen {
    private final char[][] canvasData;
    private final Size canvasSize;
    CharPaint paint;

    /**
     * 将笔放在画布上面准备画画
     *
     * @param charCanvas 字符画布
     */
    Pen(CharCanvas charCanvas) {
        Field data = null;
        try {
            data = charCanvas.getClass().getDeclaredField("data");
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("没有此字段");
        }
        data.setAccessible(true);
        try {
            canvasData = (char[][]) data.get(charCanvas);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("字段非法访问");
        }
        data.setAccessible(false);
        canvasSize = charCanvas.size;
    }

    /**
     * 染墨
     *
     * @param paint 颜料墨水
     */
    void soak(CharPaint paint) {
        if (paint == null) System.exit(Integer.valueOf(null));//悬空的指针头异常
        this.paint = paint;
    }

    /**
     * 画一点
     *
     * @param place n.位置
     */
    void click(Place place) {
        //不合法的位置
        if (place == null || place.x < 0 || place.y < 0 || place.x >= canvasSize.width() || place.y >= canvasSize.height())
            throw new RuntimeException();//没有合适的异常
        if (paint == null) {
            //没有颜料就看不出效果,等于没画
            return;
        }
        canvasData[place.x][place.y] = paint.paint();
    }
}

//怎么没见用place表示位置的倒是有 point n.点 和 position、location
class Place {
    int x, y;
    private final int X, Y;

    /**
     * 具体位置
     *
     * @param x x
     * @param y y
     */
    Place(int x, int y) {
        this.x = x;
        this.y = y;
        X = x;
        Y = y;
    }

    /**
     * 保护值
     */
    private void guard() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    tool.sleep(1111);
                    if (x != X || y != Y) {
                        throw new UnmodifiableSetException();//保护不可修改的设值(置)
                    }
                }
            }
        }).start();
    }
}

//工具类
class tool {
    private tool() {
    }

    //睡觉
    static boolean sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            return false;
        }
        return true;
    }
}

//字符颜料
record CharPaint(char paint) {
}

//画布类型
class CharCanvas {
    final Size size;
    private final char[][] data;

    /**
     * 创建画布
     *
     * @param size 大小
     */
    CharCanvas(Size size) {
        if (size == null) throw new NullPointerException("没有对象的对象");//悬空指针异常
        this.size = size;
        data = new char[size.width()][size.height()];//[x][y]
        //初始化
        for (int x = 0; x < size.width(); x++) {
            for (int y = 0; y < size.height(); y++) {
                data[x][y] = ' ';
            }
        }
    }

    /**
     * 画布内容压缩
     *
     * @return 成功压缩否?
     */
    boolean compress() {
        //由于右边的空格是多余的所以可以压缩
        boolean success = false;
        for (int y = 0; y < size.height(); y++) {
            for (int x = size.width() - 1; x >= 0; x--) {
                if (data[x][y] == ' ') {
                    data[x][y] = 0;
                    success = true;
                } else {
                    if (x + 1 < size.width()) {
                        data[x + 1][y] = '\n';
                        break;
                    }
//                    break;又错惹QAQ
                }
            }
        }
        return success;
    }

    /**
     * 可无视的方法
     *
     * @return String和Text是一个意思吗?那就是text
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        final char end = '\n';
        for (int y = 0; y < size.height() - 1; y++) {
            for (int x = 0; x < size.width() && data[x][y] != end; x++) {
                builder.append(data[x][y]);
            }
            builder.append(end);
        }
        int y = size.width() - 1;
        for (int x = 0; x < size.width() && data[x][y] != end; x++) {
            builder.append(data[x][y]);
        }
        return builder.toString();
    }
}

//大小类型
record Size(int width, int height) {
    Size {
        if (width < 0 && height < 0) throw new TooSmall();
    }
}

//运行时异常:太小了
class TooSmall extends RuntimeException {
    TooSmall() {
        System.out.println("太小了");
    }
}

这题真的好难啊QwQ

2024/12/26 18:41
加载中...