2013-11-15 173 views
0

为什么我在运行下面的代码块时没有打印到屏幕上? 它不准确地打印它,看起来像是我在扫描坐标时做错的事情。打印圆圈到屏幕

public class Question2 { 

    public static void main(String[] args) { 
     DrawMeACircle(3, 3, 3); // should print to the screen an ellipse looking circle 
    } 

    public static void DrawMeACircle(double posX, double posY, double radius) { 

     double xaxis = 20; // scanning the coordinates 
     double yaxis = 20; // " " 

     for (double x = 0; x < xaxis; x++) { 
      for (double y = 0; y < yaxis; y++) { 

       //using the formula for a cicrle 
       double a = Math.abs((posX - x) * (posX - x)); 
       double b = Math.abs((posY - y) * (posY - y)); 

       double c = Math.abs(a + b);   
       double d = Math.abs(radius * radius); 

       // checking if the formula stands correct at each coordinate scanned 
       if (c == d) { 
        System.out.print('#'); 
       } 
       else { 
        System.out.print(' '); 
       } 
      } 
      System.out.println(); 
     } 
    } 
} 
+1

它打印什么? –

+1

什么不起作用?是不是正在绘制,还是印刷的东西看起来不像是一个圆圈? – imulsion

+0

这似乎工作,但它是一个粗糙的表示,因为你打印到控制台 – Sionnach733

回答

2

恐怕比较双打这样

if (c == d) { 

    System.out.print('#'); 
    } 

是非常不可靠的,他们很可能通过一点点丢失,而你不打印,你需要到圆。

我建议你检查的范围,而不是

double arbitraryNumber = 2; 
    if (math.abs(c - d) < arbitraryNumber)) { 

    System.out.print('#'); 
    } 

或者,如果你想有一个更可靠的方式,我会做一个2-d字符数组,并把它作为一个坐标系,然后用圆圈填充二维数组并打印数组。

您可以使用一点点三角函数来填充阵列。只要找出点应该在几度(或弧度)的位置,直到你已经走完了360度

+0

我用int试过了,它给了我相同的结果。它基本上打印了圆的轮廓,但实际上并没有在其应有的位置打印“哈希”。 – user2997263

+1

你提供的比较是错误的...真的错了。大概意思是'math.abs(c - d)<任意数字' – Zong

+0

我没有完全明白你们的意思,请你解释一下吗? – user2997263