Java 基础练习:控制台画圆,画等腰三角,乘法表

Java 基础练习:控制台画圆,画等腰三角,乘法表

画圆

效果

在这里插入图片描述在这里插入图片描述

代码

package FourPointSeven;

public class Circular {
    public static void main(String[] args) {
        paintCircular(6);
    }

    /**
     * 控制台输出一个圆
     * @param R 圆的半径
     */
    private static void paintCircular(int R){
        int x1, x2;
        int r = R+1;
        for (var i = 0;i < r-2; i++){
            x1 = (r - (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
            x2 = (2 * (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
            for (var j = 0; j <= x1;j++){
                System.out.print("  ");
            }
            System.out.print("*");
            for (var k = 0; k < x2;k++){
                System.out.print("  ");
            }
            System.out.print("*");
            System.out.print("\n");
        }
        for (var i = r-2;i >= 0; i--){
            x1 = (r - (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
            x2 = (2 * (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
            for (var j = 0; j <= x1;j++){
                System.out.print("  ");
            }
            System.out.print("*");
            for (var k = 0; k < x2;k++){
                System.out.print("  ");
            }
            System.out.print("*");
            System.out.print("\n");
        }
    }
}

画等腰三角形

效果

在这里插入图片描述

代码

package FourPointSeven;

public class IsoscelesTriangle {
    public static void main(String[] args){
        paintIsoscelesTriangle(6);
    }
    /**
     * 控制台输出等腰三角形
     * @param len 等腰三角型低边的长度
     */
    private static void paintIsoscelesTriangle(int len){
        for (var i = 0; i < (len*2-1); i=i+2) {
            for (var j = 0; j < (len*2-1 - i)/2; j++){
                System.out.print(" ");
            }
            for (var k = 0;k <= i;k++){
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

乘法表

效果

在这里插入图片描述

代码

package FourPointSeven;

public class MultiplicationTable {
    public static void main(String[] args) {
        for (var i = 1; i <= 9; i++) {
            for (var j = 1; j <= i;j++){
                System.out.printf("%dX%d=%d",i,j,i*j);
                if (j < i){
                    System.out.print(",");
                }
            }
            System.out.print("\n");
        }
    }
}

posted @ 2019-08-30 20:11  消灭猕猴桃  阅读(202)  评论(0编辑  收藏  举报