吴一尘 2017-04-20 09:32 采纳率: 80%
浏览 8821
已采纳

提示在类中找不到 main 方法!!!!

代码如下: class AreaAndLength{
double sideA,sideB,sideC,area,length;
boolean b;
public AreaAndLength(double a,double b,double c){
sideA=a;
sideB=b;
sideC=c;
if(a+b>c)
{
this.b=true;
}
else{
this.b=false;
}
}
double getLength(){
return sideA+sideB+sideC;
}
public double getArea(){
if(b){
double p=(sideA+sideB+sideC)/2.0;
area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
return area;
}
else{
System.out.println("不是一个三角形,不能计算面积");
return 0;
}
}
}
class Rectangle{
double width,height,area;
Rectangle(double w,double h){
width=w;
height=h;
}
double getLength(){
return (width+height)*2;
}
double getArea(){
return width*height;
}
}
class Circle{
double radius,area;
Circle(double r){
radius=r;
}
double getLength(){
return 6.18*radius;
}
double getArea(){
return 3.14*radius*radius;
}
}
class AreaAndLength{
double length,area;
Circle circle=null;
AreaAndLength trangle=null;
Rectangle rectangle=null;
Circle cicle=new Circle(6.0);
AreaAndLength trangle=new AreaAndLength(3.0,2.0,2.0);
Rectangle rectangle=new Rectangle(3.0,5.0);
public static void main(String[] args){

length=circle.getLength();
System.out.println("圆的周长:"+length);
area=circle.getArea();
System.out.println("圆的面积:"+area);
length=trangle.getLength();
System.out.println("三角形的周长:"+length);
area=trangle.getArea();
System.out.println("三角形的面积:"+area);
area=rectangle.getArea();
System.out.println("矩形的面积:"+area);
length=rectangle.getLength();
System.out.println("矩形的周长:"+length);
}
}

  • 写回答

13条回答 默认 最新

  • 取经修身齐天下 2017-04-20 11:23
    关注

    /*出現"提示在类中找不到 main方法"的原因是:創建對象的位置不對,應該在主方法內去創建*/
    public class Demo {
    static double length, area;
    public static void main(String[] args) {
    Circle circle = new Circle(6.0);//圓
    AreaAndLength aal=new AreaAndLength(2,3,3);//三角形
    Rectangle re=new Rectangle(5,3);//矩形

        length = circle.getLength();
        System.out.println("圆的周长:" + length);//37.08
        area = circle.getArea();
        System.out.println("圆的面积:" + area);
        length = aal.getLength();
        System.out.println("三角形的周长:" + length);
        area = aal.getArea();
        System.out.println("三角形的面积:" + area);
        area = re.getArea();
        System.out.println("矩形的面积:" + area);
        length = re.getLength();
        System.out.println("矩形的周长:" + length);
    }
    

    }
    class AreaAndLength {
    double sideA, sideB, sideC, area, length;
    boolean b;

    public AreaAndLength(double a, double b, double c) {
        sideA = a;
        sideB = b;
        sideC = c;
        if (a + b > c) {
            this.b = true;
        } else {
            this.b = false;
        }
    }
    
    double getLength() {
        return sideA + sideB + sideC;
    }
    
    public double getArea() {
        if (b) {
            double p = (sideA + sideB + sideC) / 2.0;
            area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
            return area;
        } else {
            System.out.println("不是一个三角形,不能计算面积");
            return 0;
        }
    }
    

    }
    class Rectangle {
    double width, height, area;

    Rectangle(double w, double h) {
        width = w;
        height = h;
    }
    /*矩形面積的周長方法*/
    double getLength() {
        return (width + height)*2;
    }
    /*矩形面積的方法*/
    double getArea() {
        return width*height;
    }
    

    }
    class Circle {
    double radius, area;

     Circle(double r) {
        radius = r;
    }
     /*圓的周長方法*/
    double getLength() {
        return 6.18 * radius;
    }
    /*圓的面積方法*/
    double getArea() {
        return 3.14 * radius * radius;
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了