...成员包括length、width、height。要求用成员函数实现
发布网友
发布时间:2024-10-21 22:37
我来回答
共1个回答
热心网友
时间:2024-10-26 14:26
import java.util.Scanner;
public class Rectangular {
private double length, width, height;
public Rectangular(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
public String getAttribute() {
return "长方体的长为:" + length + " 宽为:" + width + " 高为:" + height;
}
public double getVolume() {
return (length * width * height);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入长宽高");
double length, width, height;
length = scanner.nextDouble();
width = scanner.nextDouble();
height = scanner.nextDouble();
Rectangular rectangular = new Rectangular(length, width, height);
System.out.println(rectangular.getAttribute());
System.out.println("长方体的体积是" + rectangular.getVolume());
}
}