public class Du2 {
public static void main(String args[]) {
//第壹個長繁體
double[] ary = getCuboidLengthFromInput();
printCuboidAreaAndCubage(ary);
//第二個長方體
double[] ary2 = getCuboidLengthFromInput();
printCuboidAreaAndCubage(ary2);
////第3個長方體
double[] ary3 = getCuboidLengthFromInput();
printCuboidAreaAndCubage(ary3);
}
private static void printCuboidAreaAndCubage(double[] ary) {
Cuboid first = new Cuboid(ary[0], ary[1], ary[2]);
double area = first.getArea();
double cubage = first.getCubage();
System.out.println("長方體的表面積為: " + area);
System.out.println("長方體的體積為:" + cubage);
System.out.println();
}
private static double[] getCuboidLengthFromInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入長方體的長:");
double length = scanner.nextDouble();
System.out.print("請輸入長方體的寬:");
double width = scanner.nextDouble();
System.out.print("請輸入長方體的高:");
double height = scanner.nextDouble();
double[] ary = {length, width, height};
return ary;
}
}
class Cuboid{//長方體類
private double length;//長
private double width;//寬
private double height;//高
public Cuboid(double length, double width, double height){
this.length = length;
this.width = width;
this.height = height;
}
public double getArea(){
return 2 * (length * width + length * height + width * height);
}
public double getCubage(){
return length * width * height;
}
}
-----------------------
請輸入長方體的長:2
請輸入長方體的寬:4
請輸入長方體的高:5
長方體的表面積為: 76.0
長方體的體積為:40.0
請輸入長方體的長:5
請輸入長方體的寬:8
請輸入長方體的高:20
長方體的表面積為: 600.0
長方體的體積為:800.0
請輸入長方體的長:3
請輸入長方體的寬:6
請輸入長方體的高:9
長方體的表面積為: 198.0
長方體的體積為:162.0