• 绘制指定半径的圆


    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();
            }
        }
    }
  • 相关阅读:
    Java中XML的四种解析方式(二)
    Java中XML的四种解析方式(一)
    Java反射机制
    创建线程的三种方式(Thread、Runnable、Callable)
    GBK和UTF-8的区别
    selenium的其他方法
    selenium
    XPath
    mysqldump / MySQL 备份与恢复语句
    MySQL 增删改查语句/SELECT INSET INTO UPDATE
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4573895.html
Copyright © 2020-2023  润新知