用Java编写这个程序怎么写

用户自定义半径的圆形和用户自定义边长的正方形。
这个怎么做用Java,救救孩子




如果不要求画出实际的圆形和正方形,就建立两个class,分别代表圆和正方形,内部设置对应的长度/半径变量即可
类似这种

class Circle {
  private double radius;

  public double getRadius() {
    return radius;
  }

  public void setRadius(double radius) {
    this.radius = radius;
  }

}

不是很简单嘛,代码如下

public class Test {
    public static void main(String[] args) {
        //调用打印矩形方法
        rect();
        //调用打印圆形方法
      //  circle();
    }
    //实现自定义矩形的方法
    public static void rect(){
        //控制台输入
        Scanner sc=new Scanner(System.in);
        //自定义长度和高度
        System.out.println("请输入长度:");
        int length= sc.nextInt();
        System.out.println("请输入高度:");
        int height=sc.nextInt();
        for (int i=0;i<height;i++) {
            for (int j=0;j<length;j++) {
                if ( i == 0 ) {
                    System.out.print("*");
                }
                else if (i == height-1) {
                    System.out.print("*");
                }
                else if (j == 0 || j == length-1) {
                    System.out.print("*");
                }
                else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }
    //实现自定义圆方法
    public static void circle() {
        //控制台输入
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入半径:");
        //自定义长度和高度
        int r= sc.nextInt();
        for (int y = 0; y <= 2 * r; y += 2) {
            int x = (int) Math.round(r - Math.sqrt(2 * r * y - y * y));
            int len = 2 * (r - x);
            // 圆左的空白
            for (int i = 0; i <= x; i++) {
                System.out.print(' ');
            }
            // 左半圆
            System.out.print('*');
            // 中间空白
            for (int j = 0; j <= len; j++) {
                System.out.print(' ');
            }
            // 右半圆
            System.out.println('*');
        }
    }
}

测试自定义矩形结果

img

测试自定义圆形结果

img