♀木子 2021-11-17 21:28 采纳率: 100%
浏览 48
已结题

😔😔用idea写代码,涉及到知识盲区,想问一下具体过程!🙏🙏

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-11-17 21:42
    关注
    
    import java.util.*;
    interface Shape{
        float getArea();                            //求面积
        float getCircumference();                   //求周长
    }
    
    //Circle类
    class Circle implements Shape{
        private final float PI=3.14f;
        private float radius;
    
        Circle(){}                                  //Circle类构造方法
        Circle(float r){
            radius=r;
        }
        public float getArea(){                     //Circle类求面积
            return PI*radius*radius;
        }
        public float getCircumference(){            //Circle类求周长
            return 2*PI*radius;
        }
    }
    
    //Rectangle类
    class Rectangle implements Shape{
        private float width;
        private float height;
    
        Rectangle(){}                               //Rectangle类构造方法
        Rectangle(float width,float height){
            this.width=width;
            this.height=height;
        }
        public float getArea(){                     //Rectangle类求面积
            return width*height;
        }
        public float getCircumference(){            //Rectangle类求周长
            return 2*(width+height);
        }
    }
    
    //Square类,继承Rectangle类
    class Square extends Rectangle{
        Square(){}                                  //Square类构造方法
        Square(float length){
            super(length,length);
        }
    
    }
    class ShapeInterface {
    
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
    
            Shape shape=null;
            //得写“null”,不然输出句会显示说shape:“The local variable shape may not have been initialized”,它是考虑到else中没有初始化shape对象,虽然从我们的逻辑上看在运行中是不会有这样的问题的……
    
            int y;
            do{
                y=0;
                System.out.println("请选择输入的图形:1、圆形,2、矩形,3、正方形:");
                int x=sc.nextInt();
                //圆形
                if(x==1){
                    System.out.println("请输入圆形的半径:");
                    float radius=sc.nextFloat();
    
                    shape=new Circle(radius);
                }
                //矩形
                else if(x==2){
                    System.out.println("请输入矩形的长和宽,以空格相隔:");
                    float width=sc.nextFloat();
                    float height=sc.nextFloat();
    
                    shape=new Rectangle(width,height);      
                }
                //正方形
                else if(x==3){
                    System.out.println("请输入正方形的边长:");
                    float length=sc.nextFloat();
    
                    shape=new Square(length);
                }
                //输入错误
                else{
                    y=1;
                    System.out.println("输入错误,请重新输入:");
                }
            }
            while(y==1);
    
            System.out.println("面积:"+shape.getArea()+"\t\t周长:"+shape.getCircumference());
    
            sc.close();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月25日
  • 已采纳回答 11月17日
  • 创建了问题 11月17日

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao