英文:
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 <= 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
得分: 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 ) ){ <---- 可能是你的问题处
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 ) ){ <---- 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");
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论