Java双重循环和条件使用stdDraw绘制图片,找不到错误。

huangapple go评论66阅读模式
英文:

Java double for-loop and condition usng stdDraw to draw a picture, can not find the failure

问题

我因为大学关闭了所以在家学习Java最近我试图使用StdDraw绘制图片如您在下面的图片中所见但我不明白为什么中间的红色圆圈丢失了希望您能帮助我谢谢

import java.awt.*;

public class 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;
        }
    }
}
英文:

I am learning java at home since university has been closed. recently I am trying to draw a picture using StdDraw as you see in the picture below, but I don't understand, why the red circle in the middle is missing, I hope you could help me, thank you!

one red circle in the middle is missing

import java.awt.*;

public class 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 &lt;= n; i++) {
for (int j = 1; j &lt;= 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 &gt;= 2 &amp;&amp; j &lt; 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 &gt;= 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 + &quot;=i&quot;);
}
} 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

得分: 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 )  ){    &lt;---- 可能是你的问题处
        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);
    }
}

虽然不保证,但根据你的代码,这可能是原因。
英文:

I think this might be your problem.

                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 )  ){    &lt;---- 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 + &quot;=i&quot;);
}

There is one case in which i and j are the same value. In your case it is 4.
If i and j both equals to 4 the code will always go into the first if (j == n/2) and will not execute the else if... So basically the red circle you asked for will not be drawn.

Btw: The n%2==0 check is practically useless because you are not changing the value of n (it is always 8). So your last code will never be executed:

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

No guarantee though but based on your code that might be the reason.

huangapple
  • 本文由 发表于 2020年4月6日 22:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定