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

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

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

问题

  1. 我因为大学关闭了所以在家学习Java最近我试图使用StdDraw绘制图片如您在下面的图片中所见但我不明白为什么中间的红色圆圈丢失了希望您能帮助我谢谢
  2. import java.awt.*;
  3. public class draw1 {
  4. public static void main(String[] args) {
  5. int n = 8;
  6. int width = 300;
  7. int height = 300;
  8. int circle_x = width / n / 2;
  9. int circle_y = height / n / 2;
  10. int radius = 300 / n / 2;
  11. StdDraw.setCanvasSize(width, height);
  12. StdDraw.setXscale(0, 300);
  13. StdDraw.setYscale(0, 300);
  14. StdDraw.setPenRadius(0.002);
  15. StdDraw.setPenColor(Color.blue);
  16. for (int i = 1; i <= n; i++) {
  17. for (int j = 1; j <= n; j++) {
  18. if (i == 1 || i == n) {
  19. if (j == 1 || j == n) {
  20. StdDraw.setPenColor(Color.blue);
  21. StdDraw.setPenRadius(0.002);
  22. StdDraw.circle(circle_x, circle_y, radius);
  23. } else if (j >= 2 && j < n) {
  24. StdDraw.setPenColor(Color.red);
  25. StdDraw.setPenRadius(0.006);
  26. StdDraw.circle(circle_x, circle_y, radius);
  27. }
  28. } else {
  29. if (j == 1 || j == n) {
  30. StdDraw.setPenColor(Color.blue);
  31. StdDraw.setPenRadius(0.002);
  32. StdDraw.circle(circle_x, circle_y, radius);
  33. } else if (j >= 2 ) {
  34. if (n % 2 == 0) {
  35. if (j == n / 2) {
  36. StdDraw.setPenColor(Color.orange);
  37. StdDraw.setPenRadius(0.006);
  38. StdDraw.circle(circle_x + radius, circle_y, radius);
  39. } else if ( (i == n / 2 ) ){
  40. StdDraw.setPenColor(Color.red);
  41. StdDraw.setPenRadius(0.006);
  42. StdDraw.circle(circle_x, circle_y + radius, radius);
  43. System.out.println(i + "=i");
  44. }
  45. } else if (n % 2 != 0) {
  46. if (j == n / 2 + 1) {
  47. StdDraw.setPenColor(Color.orange);
  48. StdDraw.setPenRadius(0.006);
  49. StdDraw.circle(circle_x, circle_y, radius);
  50. } else if (i == n / 2 + 1) {
  51. StdDraw.setPenColor(Color.orange);
  52. StdDraw.setPenRadius(0.006);
  53. StdDraw.circle(circle_x, circle_y, radius);
  54. }
  55. }
  56. }
  57. }
  58. System.out.println(j);
  59. circle_x = circle_x + (radius * 2);
  60. }
  61. circle_x = width / n / 2;
  62. circle_y = circle_y + width / n;
  63. }
  64. }
  65. }
英文:

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 {

  1. public static void main(String[] args) {
  2. int n = 8;
  3. int width = 300;
  4. int height = 300;
  5. int circle_x = width / n / 2;
  6. int circle_y = height / n / 2;
  7. int radius = 300 / n / 2;
  8. StdDraw.setCanvasSize(width, height);
  9. StdDraw.setXscale(0, 300);
  10. StdDraw.setYscale(0, 300);
  11. StdDraw.setPenRadius(0.002);
  12. StdDraw.setPenColor(Color.blue);
  13. for (int i = 1; i &lt;= n; i++) {
  14. for (int j = 1; j &lt;= n; j++) {
  15. if (i == 1 || i == n) {
  16. if (j == 1 || j == n) {
  17. StdDraw.setPenColor(Color.blue);
  18. StdDraw.setPenRadius(0.002);
  19. StdDraw.circle(circle_x, circle_y, radius);
  20. } else if (j &gt;= 2 &amp;&amp; j &lt; n) {
  21. StdDraw.setPenColor(Color.red);
  22. StdDraw.setPenRadius(0.006);
  23. StdDraw.circle(circle_x, circle_y, radius);
  24. }
  25. } else {
  26. if (j == 1 || j == n) {
  27. StdDraw.setPenColor(Color.blue);
  28. StdDraw.setPenRadius(0.002);
  29. StdDraw.circle(circle_x, circle_y, radius);
  30. } else if (j &gt;= 2 ) {
  31. if (n % 2 == 0) {
  32. if (j == n / 2) {
  33. StdDraw.setPenColor(Color.orange);
  34. StdDraw.setPenRadius(0.006);
  35. StdDraw.circle(circle_x + radius, circle_y, radius);
  36. } else if ( (i == n / 2 ) ){
  37. StdDraw.setPenColor(Color.red);
  38. StdDraw.setPenRadius(0.006);
  39. StdDraw.circle(circle_x, circle_y + radius, radius);
  40. System.out.println(i + &quot;=i&quot;);
  41. }
  42. } else if (n % 2 != 0) {
  43. if (j == n / 2 + 1) {
  44. StdDraw.setPenColor(Color.orange);
  45. StdDraw.setPenRadius(0.006);
  46. StdDraw.circle(circle_x, circle_y, radius);
  47. } else if (i == n / 2 + 1) {
  48. StdDraw.setPenColor(Color.orange);
  49. StdDraw.setPenRadius(0.006);
  50. StdDraw.circle(circle_x, circle_y, radius);
  51. }
  52. }
  53. }
  54. }
  55. System.out.println(j);
  56. circle_x = circle_x + (radius * 2);
  57. }
  58. circle_x = width / n / 2;
  59. circle_y = circle_y + width / n;
  60. }
  61. }
  62. }

答案1

得分: 0

  1. 我认为这可能是你的问题。
  2. if (n % 2 == 0) {
  3. if (j == n / 2) {
  4. StdDraw.setPenColor(Color.orange);
  5. StdDraw.setPenRadius(0.006);
  6. StdDraw.circle(circle_x + radius, circle_y, radius);
  7. } else if ( (i == n / 2 ) ){ &lt;---- 可能是你的问题处
  8. StdDraw.setPenColor(Color.red);
  9. StdDraw.setPenRadius(0.006);
  10. StdDraw.circle(circle_x, circle_y + radius, radius);
  11. System.out.println(i + "=i");
  12. }
  13. }
  14. 在某种情况下,ij的值相同。在你的情况下,它是4
  15. 如果ij都等于4,则代码将始终进入第一个 `if (j == n/2)`,而不会执行 `else if...`。所以基本上你要求的红色圆将不会被绘制。
  16. 顺便说一下:`n%2==0` 的检查实际上是无用的,因为你没有改变n的值(它始终为8)。因此,你的最后一段代码永远不会被执行:
  17. } else if (n % 2 != 0) {
  18. if (j == n / 2 + 1) {
  19. StdDraw.setPenColor(Color.orange);
  20. StdDraw.setPenRadius(0.006);
  21. StdDraw.circle(circle_x, circle_y, radius);
  22. } else if (i == n / 2 + 1) {
  23. StdDraw.setPenColor(Color.orange);
  24. StdDraw.setPenRadius(0.006);
  25. StdDraw.circle(circle_x, circle_y, radius);
  26. }
  27. }
  28. 虽然不保证,但根据你的代码,这可能是原因。
英文:

I think this might be your problem.

  1. if (n % 2 == 0) {
  2. if (j == n / 2) {
  3. StdDraw.setPenColor(Color.orange);
  4. StdDraw.setPenRadius(0.006);
  5. StdDraw.circle(circle_x + radius, circle_y, radius);
  6. } else if ( (i == n / 2 ) ){ &lt;---- HERE MIGHT BE YOUR PROBLEM
  7. StdDraw.setPenColor(Color.red);
  8. StdDraw.setPenRadius(0.006);
  9. StdDraw.circle(circle_x, circle_y + radius, radius);
  10. System.out.println(i + &quot;=i&quot;);
  11. }

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:

  1. } else if (n % 2 != 0) {
  2. if (j == n / 2 + 1) {
  3. StdDraw.setPenColor(Color.orange);
  4. StdDraw.setPenRadius(0.006);
  5. StdDraw.circle(circle_x, circle_y, radius);
  6. } else if (i == n / 2 + 1) {
  7. StdDraw.setPenColor(Color.orange);
  8. StdDraw.setPenRadius(0.006);
  9. StdDraw.circle(circle_x, circle_y, radius);
  10. }
  11. }

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:

确定