提问者:小点点

Java双for循环和条件使用stdDraw绘制图片,找不到失败


最近我在尝试用StdDraw画画,就像你看到的下图一样,但是我不明白,为什么中间的红色圆圈不见了,希望你能帮助我,谢谢!

中间少了一个红圈

导入java. awt。*;

公共类绘图1{

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;


            }


        }

    }

共1个答案

匿名用户

我想这可能是你的问题。

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

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