0

由于大学已经关闭,我正在家里学习java。最近我正在尝试使用StdDraw画一张图片,如下图所示,但我不明白,为什么中间的红色圆圈不见了,希望你能帮助我,谢谢!

中间少了一个红圈

导入 java.awt.*;

公共类draw1 {

public static void main(String[] args) {
    int n = 8;
    int width = 300;
    int height = 300;
    int circle_x = width / n / 2;
    int circle_y = height / n / 2;
    int radius = 300 / n / 2;

    StdDraw.setCanvasSize(width, height);
    StdDraw.setXscale(0, 300);
    StdDraw.setYscale(0, 300);
    StdDraw.setPenRadius(0.002);
    StdDraw.setPenColor(Color.blue);

    for (int i = 1; i <= n; i++) {


        for (int j = 1; j <= n; j++) {
            if (i == 1 || i == n) {
                if (j == 1 || j == n) {
                    StdDraw.setPenColor(Color.blue);
                    StdDraw.setPenRadius(0.002);
                    StdDraw.circle(circle_x, circle_y, radius);
                } else if (j >= 2 && j < n) {
                    StdDraw.setPenColor(Color.red);
                    StdDraw.setPenRadius(0.006);
                    StdDraw.circle(circle_x, circle_y, radius);
                }


            } else  {
                if (j == 1 || j == n) {
                    StdDraw.setPenColor(Color.blue);
                    StdDraw.setPenRadius(0.002);
                    StdDraw.circle(circle_x, circle_y, radius);
                } else if (j >= 2 ) {
                    if (n % 2 == 0) {
                        if (j == n / 2) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x + radius, circle_y, radius);



                        } else if ( (i == n / 2 )  ){
                            StdDraw.setPenColor(Color.red);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y + radius, radius);

                            System.out.println(i + "=i");

                        }
                    } else if (n % 2 != 0) {
                        if (j == n / 2 + 1) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y, radius);
                        } else if (i == n / 2 + 1) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y, radius);
                        }
                    }


                }
            }
            System.out.println(j);
                    circle_x = circle_x + (radius * 2);
                }
                circle_x = width / n / 2;
                circle_y = circle_y + width / n;


            }


        }

    }
4

1 回答 1

0

我想这可能是你的问题。

                if (n % 2 == 0) {
                    if (j == n / 2) {
                        StdDraw.setPenColor(Color.orange);
                        StdDraw.setPenRadius(0.006);
                        StdDraw.circle(circle_x + radius, circle_y, radius);



                    } else if ( (i == n / 2 )  ){    <---- HERE MIGHT BE YOUR PROBLEM
                        StdDraw.setPenColor(Color.red);
                        StdDraw.setPenRadius(0.006);
                        StdDraw.circle(circle_x, circle_y + radius, radius);

                        System.out.println(i + "=i");

                    }

有一种情况,其中 i 和 j 是相同的值。在您的情况下,它是 4。如果 i 和 j 都等于 4,则代码​​将始终进入第一个if (j == n/2) 并且不会执行else if...所以基本上您要求的红色圆圈不会被绘制。

顺便说一句:n%2==0检查实际上没用,因为您没有更改 n 的值(始终为 8)。所以你最后的代码永远不会被执行:

                } else if (n % 2 != 0) {
                    if (j == n / 2 + 1) {
                        StdDraw.setPenColor(Color.orange);
                        StdDraw.setPenRadius(0.006);
                        StdDraw.circle(circle_x, circle_y, radius);
                    } else if (i == n / 2 + 1) {
                        StdDraw.setPenColor(Color.orange);
                        StdDraw.setPenRadius(0.006);
                        StdDraw.circle(circle_x, circle_y, radius);
                    }
                }

虽然不能保证,但根据您的代码可能是原因。

于 2020-04-06T14:31:50.987 回答