当前位置: 首页 > 知识库问答 >
问题:

如何将音量从getVolume调用到isBigbox方法中?

冯文彬
2023-03-14

如何将volume从getVolume调用到isBigbox方法中,以编写检查Box vlomue是否大于或等于500的If语句?

正如你在标题中读到的,我试图在isBigbox方法中使用getVolume方法中的卷来检查卷是否小于或等于500,它将打印一条语句,说明它的大盒子,否则它将显示它是一个小盒子

public class Box {

    private double length;
    private double width;
    private double height;
    private String color;

    public Box(double length, double width, double height, String color) {
        this.length = length;
        this.width = width;
        this.height = height;
        this.color = color;
    }

    // Overload constructor 1
    public Box() {
        this(1, 1, 1, "without color");
    }

    // Overload constructor 2
    public Box(double length, double width, double height) {
        this(1, 2, 3, "blue");
    }

    // Overload constructor 3
    public Box(double width, double height, String color) {
        this(5, 5, 8, "red");
    }

    // Standard getters and setters omitted for brevity

    public void getVolume(double volume) {
        volume = this.height * this.width * this.height;
        System.out.println("The Volume of the box is:" + volume);
    }
      
    public void isBigBox(Box b) {
        //
    }
}

共有2个答案

颜嘉誉
2023-03-14

调用方法getVolume,但它不返回任何内容(void)。您应该拆分这些方法,然后直接调用它们:

public double getVolume() {
    return this.height * this.width * this.length;      
}

public void printVolume() {
    System.out.println("The Volume of the box is:" + this.getVolume());      
}

isBigBox中,您可以简单地检查音量:

public boolean isBigBox() {
    return this.getVolume() > 500;
}
贲文景
2023-03-14

您将需要在类Box中创建称为卷的变量并使构造函数计算卷。你需要这样的东西:

 public class Box {
    
    private double length;
    private double width;
    private double height;
    private String color;
    //
    private double volume:    

    public Box(double length, double width, double height,String color){
        this.length=length;
        this.width=width;
        this.height=height;
        this.color=color;
        //
        this.volume = length*width*height;
    }
  
    // 
    public double getVolume() {
        return volume;
    }
    // 
    public boolean isBigBox(){
        return (volume >= 500); 
    }
}
 类似资料:
  • 问题内容: 在NetBeans中,如何找出在哪里调用方法? 我在一个大型项目中有一个特定的方法,无法找到它的调用位置。 问题答案: 右键单击方法名称时,会出现“ 查找用法” 菜单项。 或将光标放在方法名称上,然后按+ (查找用法)和++ + (显示“查找用法”结果)。

  • 问题内容: 我已经创建了一个applet,并且将要从Web项目上的HTML页面访问applet方法。 这里我的小程序看起来像: 我的HTML页面如下所示: 但是,当我单击单选按钮时,浏览器将挂起,并且无法访问applet方法。 我的applet类位于默认目录中,而HTML位于WebContent文件夹中。我应该更改我的代码吗? 问题答案: 问题是语句检查: 这与JavaScript不完全一样,因为

  • 我需要了解如何将方法返回到方法中,以打印出计算机的随机选择。 打印语句之后的最后一个方法不完整;我只是被这部分卡住了。

  • 问题内容: 我在Java项目中使用Jython。 我有一个Java类:和一个Python类: Python文件如下: 现在的问题是我想从Java文件的方法调用Python文件的方法并打印结果。 问题答案: 如果我没看错文档,则可以使用以下功能: 或者,如果您想获取字符串: 如果要向其提供Java变量的某些输入,则可以预先使用,而不要在Python代码中使用该变量名:

  • 问题内容: 我有一个方法,它在我的viewController中具有自定义序列,如下所示: 我在GameScene.swift中这样调用方法: 我仔细检查了segue名称,它是正确的。每当我在GameScene.swift文件中调用此命令时,我都会收到SIGABRT消息,但我不知道为什么。我尝试仅使用println()消息调用该函数,并且该函数有效。 对于为什么发生这种情况以及如何成功调用Game

  • 问题内容: 例如,我正在尝试做这样的事情 我收到一条错误消息,告诉我无法从静态环境中引用非静态变量。因此,如果这是真的,我将如何在main内部使用非静态方法? 问题答案: 你不能 非静态方法是必须在Test类的实例上调用的方法。创建Test的实例以在您的main方法中使用: