绘制指定半径的圆
package day01; /** * Created by sherry on 000019/3/19 13:55. */ /*直角坐标*/ class Coord{ /*横坐标*/ private int i; /*纵坐标*/ private int j; /*两个坐标之间的距离(坐标值必须都是正数)*/ public double getRange(Coord coord){ return Math.sqrt(Math.pow(Math.abs(this.i-coord.getI()),2)+Math.pow(Math.abs(this.j-coord.getJ()),2)); } @Override public String toString() { return "Coord{" + "i=" + i + ", j=" + j + '}'; } @Override public boolean equals(Object o) { if (o == null){ return false; } Coord coord = (Coord) o; if (this.i == coord.getI() &&this.j == coord.getJ()){ return true; }else { return false; } } @Override public int hashCode() { int result = i; result = 31 * result + j; return result; } public Coord(int i, int j) { this.i = i; this.j = j; } public int getI() { return i; } public void setI(int i) { this.i = i; } public int getJ() { return j; } public void setJ(int j) { this.j = j; } } public class DrawCircle { public static void main(String[] args) { drawCircle(20); } private static void drawCircle(int r) { /*边框长度*/ int d = 2*r+1; /*圆心坐标*/ Coord centerOfCircle = new Coord(r,r); /*任意一点坐标*/ Coord coord; for (int i = 0;i < d;i++){ for (int j = 0;j < d;j++){ coord = new Coord(j,i); if (coord.equals(centerOfCircle)){ System.out.print("A "); } else if (Math.abs(coord.getRange(centerOfCircle)-r)<0.5){ System.out.print("B "); } else { System.out.print("* "); } } System.out.println(); } } }