web xiaobai 2024-03-21 20:24 采纳率: 50%
浏览 2

(标签-java|关键词-thread)

import java.util.Scanner;
public class Yuanzhui {
     public static void main(String[] args) {
       Scanner scanner=new Scanner(System.in);      
       double r=scanner.nextDouble();
       double h=scanner.nextDouble();
       Circle c = new Circle();
       c.setRadius(r);
       Cone cone=new Cone();
       cone.setBottom(c);
       cone.setHeight(h);
       System.out.printf("底面面积和周长分别为:%.2f %.2f\n",cone.getBottom().getArea(),cone.getBottom().getLength());
       System.out.printf("体积为:%.2f\n",cone.getVolume());      
       scanner.close();
   }
}

class Circle{
    private double radius;
    
    public void setRadius(double radius) {
        this.radius=radius;
    }
    public double getRadius() {
        return this.radius;
    }
    
    public double getArea() {
        return this.radius*this.radius*Math.PI;
    }
    public double getLength() {
        return 2*this.radius*Math.PI;
    }
}

class Cone extends Circle{
    
    private double height;
    private double bottom;
    
    public Cone() {
        this.bottom=super.getArea();
        
    }
    
    public void setBottom(Circle circle) {
        this.bottom=circle.getArea();
        
        super.getLength();
        super.getArea();
    }
    
    public double getBottom() {
        return this.bottom;
    }
    public double getHeight() {
        return this.height;
    }
    
    public void setHeight(double height) {
        this.height=height;
    }
    public double getVolume() {
        return this.height*this.bottom/3.0;
    }
}

以上代码存在编译问题:
Exception in thread "main" java,lang.Error: Unresolved compilation problems:
Cannot invoke getArea() on the primitive type double
Cannot invoke getLength() on the primitive type double
The method printf(String, Object[]) in the type PrintStream is not applicable for the
arguments (String, double)
希望知道该如何解决,谢谢。

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-03-21 20:25
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    在你的代码中,有几个错误需要修复。首先,Cone 类的构造函数和 setBottom 方法中尝试对 double 类型的 heightbottom 调用 getArea()getLength(),这是不正确的,因为这些方法是 Circle 类的方法,应该对 Circle 对象调用。其次,printf 方法的参数格式有误。以下是修复后的代码:

    import java.util.Scanner;
    
    public class Yuanzhui {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            double r = scanner.nextDouble();
            double h = scanner.nextDouble();
            Circle c = new Circle();
            c.setRadius(r);
            Cone cone = new Cone();
            cone.setBottom(c);
            cone.setHeight(h);
            System.out.printf("底面面积和周长分别为:%.2f %.2f\n", c.getArea(), c.getLength());
            System.out.printf("体积为:%.2f\n", cone.getVolume());
            scanner.close();
        }
    }
    
    class Circle {
        private double radius;
    
        public void setRadius(double radius) {
            this.radius = radius;
        }
    
        public double getRadius() {
            return this.radius;
        }
    
        public double getArea() {
            return this.radius * this.radius * Math.PI;
        }
    
        public double getLength() {
            return 2 * this.radius * Math.PI;
        }
    }
    
    class Cone extends Circle {
    
        private double height;
        private double bottomArea; // 修改了变量名,从 bottom 改为 bottomArea
    
        public Cone() {
            this.bottomArea = super.getArea(); // 使用 super.getArea() 而不是 this.bottom.getArea()
        }
    
        public void setBottom(Circle circle) {
            this.bottomArea = circle.getArea();
        }
    
        public double getBottomArea() { // 更改了方法名,从 getBottom 改为 getBottomArea
            return this.bottomArea;
        }
    
        public double getHeight() {
            return this.height;
        }
    
        public void setHeight(double height) {
            this.height = height;
        }
    
        public double getVolume() {
            return this.height * this.bottomArea / 3.0;
        }
    }
    

    现在代码应该可以正确编译并运行了。注意,我修改了 Cone 类中的 bottom 变量名和相关方法,以避免与圆的面积混淆。同时,修复了 printf 方法的参数问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月21日

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡