Java 从主方法打印一个方法

Java 从主方法打印一个方法,java,printing,Java,Printing,我对java相当陌生,我想知道如何从volume方法中打印Boxvolume变量 我知道我需要将Boxvolume作为一个全局变量。如果有人能帮我的话,我将不胜感激 这是我的密码: public static void main(String[] args) { Box a = new Box(20, 30, 40); ArrayList<Box> boxes = new ArrayList<>(); boxes.add(a); for

我对
java
相当陌生,我想知道如何从
volume
方法中打印
Boxvolume
变量

我知道我需要将
Boxvolume
作为一个全局变量。如果有人能帮我的话,我将不胜感激

这是我的密码:

public static void main(String[] args) {
    Box a = new Box(20, 30, 40);

    ArrayList<Box> boxes = new ArrayList<>();
    boxes.add(a);

    for (Box bx : boxes) {
        bx.print();
    }

    double V = volume(Boxvolume);
    System.out.println(V);
}

class Box {
    int width;
    int height;
    int depth;

    public Box(int w, int h, int d) {
        this.width = w;
        this.height = h;
        this.depth = d;
    }

    public double volume(double BoxV) {
        int Boxvolume = width * height * depth;
        return Boxvolume;
    }

    public double price(double BV) {
        double Boxprice = volume(BV) * 5;
        return Boxprice;
    }

    public void print() {
        System.out.println(this.width);
        System.out.println(this.height);
        System.out.println(this.depth);
        System.out.println();
    }
}
publicstaticvoidmain(字符串[]args){
框a=新框(20,30,40);
ArrayList Box=新的ArrayList();
框。添加(a);
用于(框bx:框){
bx.print();
}
双V=体积(箱体积);
系统输出打印Ln(V);
}
类框{
整数宽度;
内部高度;
智力深度;
公用箱(整数w、整数h、整数d){
这个宽度=w;
这个高度=h;
这个深度=d;
}
公共双卷(双箱){
int-Boxvolume=宽度*高度*深度;
返回量;
}
公共双倍价格(双倍BV){
双箱价格=容量(BV)*5;
退货价格;
}
公开作废印刷品(){
System.out.println(此宽度);
System.out.println(此高度);
System.out.println(此深度);
System.out.println();
}
}

您的代码根本就错了。首先,您需要从实例变量调用volume方法,该变量是
a.volume()。另外,在音量和价格方法中不需要双重参数。Price方法已调用volume,并且您未在volume中使用BV参数

因此,正确的版本应该如下所示:

public static void main(String[] args) {

    Box a = new Box(20, 30, 40);

    ArrayList<Box> boxes = new ArrayList<>();

    boxes.add(a);

    for (Box bx : boxes) {
        bx.print();
        System.out.println(bx.volume());
    }    
}

class Box {

    int width;
    int height;
    int depth;


    public Box(int w, int h, int d) {
        this.width = w;
        this.height = h;
        this.depth = d;

    }

    public double volume() {

        int Boxvolume = width * height * depth;
        return Boxvolume;

    }

    public double price() {

        double Boxprice = volume() * 5;

        return Boxprice;
    }



    public void print() {

        System.out.println(this.width);
        System.out.println(this.height);
        System.out.println(this.depth);

        System.out.println();
    }

}
publicstaticvoidmain(字符串[]args){
框a=新框(20,30,40);
ArrayList Box=新的ArrayList();
框。添加(a);
用于(框bx:框){
bx.print();
System.out.println(bx.volume());
}    
}
类框{
整数宽度;
内部高度;
智力深度;
公用箱(整数w、整数h、整数d){
这个宽度=w;
这个高度=h;
这个深度=d;
}
公共双卷{
int-Boxvolume=宽度*高度*深度;
返回量;
}
公共双价{
双箱价格=容量()*5;
退货价格;
}
公开作废印刷品(){
System.out.println(此宽度);
System.out.println(此高度);
System.out.println(此深度);
System.out.println();
}
}

建议您遵循Java命名约定并删除不必要的参数

本例为您的
Box
提供了一个
toString()
打印方法,删除了不必要的参数,根据Java命名约定给出变量名称,并在其他类方法中使用类方法
getVolume()
getPrice()
。如果类属性为
height
width
depth
,则体积或价格不需要其他属性,只需使用
height
width
depth
或类方法返回结果即可:

class Box {

    int width;
    int height;
    int depth;

    public Box(int w, int h, int d) {
        this.width = w;
        this.height = h;
        this.depth = d;
    }

    /*
     * - renamed the method
     * - removed the dispensable parameter
     */
    public double getVolume() {
        int Boxvolume = width * height * depth;
        return Boxvolume;
    }

    /*
     * - renamed the method
     * - removed the dispensable parameter
     */
    public double getPrice() {
        // use the getVolume() method
        double Boxprice = this.getVolume() * 5;
        return Boxprice;
    }

    public void print() {
        // just print the toString() method
        System.out.println(this.toString());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("Height: ");
        // here you take the class attributes
        sb.append(height);
        sb.append("\n");
        sb.append("Depth: ");
        sb.append(depth);
        sb.append("\n");
        sb.append("Width: ");
        sb.append(width);
        sb.append("\n");
        sb.append("Price: ");
        // here you just take your method
        sb.append(getPrice());
        sb.append("\n");
        sb.append("Volume: ");
        // so you do here
        sb.append(getVolume());
        sb.append("\n");

        return sb.toString();
    }
}
然后,您可以检查具有
main
方法的类中的输出:

import java.util.ArrayList;

public class PasteClass {

    public static void main(String[] args) {
        Box a = new Box(20, 30, 40);
        Box b = new Box(10, 20, 30);

        ArrayList<Box> boxes = new ArrayList<>();
        boxes.add(a);
        boxes.add(b);

        for (Box bx : boxes) {
            bx.print();
        }
    }
}
import java.util.ArrayList;
公共类粘贴类{
公共静态void main(字符串[]args){
框a=新框(20,30,40);
框b=新框(10,20,30);
ArrayList Box=新的ArrayList();
框。添加(a);
框。添加(b);
用于(框bx:框){
bx.print();
}
}
}

对于初学者来说,您的代码几乎是正确的:)

在你接近解决方案的过程中,似乎有一点概念上的误解。如果我过于简单化,或者假设你知道的比你知道的少,我道歉,但过度解释似乎比解释不足要好

首先是
main()
方法。由于它是
静态的
,这意味着它只有一个——你可以把它想象成一个大老板,控制小应用程序中发生的事情

这个
main()
方法(boss)创建了一个
,它是一个“东西”的单个实例。老板知道这件事,并称他的箱子为
a
。他将这个盒子添加到一个集合中,目前这个集合中只有一个盒子
a
,但将来可能会有很多盒子。我相信你已经了解了所有这些,只是把它作为背景

类可以做一些事情。它知道自己的
宽度
高度
深度
,并且知道如何根据这些属性计算
体积
价格
。它还可以
在系统控制台上打印
自身的表示

main
方法中,创建名为
a
的框后,您需要询问该框的音量。要做到这一点,您必须对感兴趣的方框实例调用
volume
方法

所以,你不仅仅要求任何音量,你要求的是box
a
的音量。您可以使用以下语法执行此操作:

a.volume();
您不需要将任何内容传递到
volume
方法中(因此,您应该从
volume
方法中删除
双框v
,因为
volume
方法是
本身的一部分,因此它已经知道
所知道的一切-
宽度
高度
深度
,这是它能够计算的全部吃了这本书

因此,您需要做一些更改:

volume
方法不需要采用任何参数,只需:

public double volume() {
  return width * height * depth;
}
(请注意,您也不需要将其分配给
int-Boxvolume
,可以删除该变量-或者如果愿意,完全由您自己决定)

main
方法中完成此操作后,您可以使用以下工具获取并打印box
a
的体积:

double volume = a.volume();
System.out.println(volume);
如果你想打印v
for (Box bx : boxes) {
  bx.print();
  double volume = bx.volume();
  System.out.println(volume);
}