隨便寫了下,希望對你有幫助!
創(chuàng)新互聯(lián)專注于泗洪企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。泗洪網(wǎng)站建設(shè)公司,為泗洪等地區(qū)提供建站服務(wù)。全流程按需定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Volume {
private final static int count = 3;
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i count; i++) {
sb.append(readString(i + 1));
sb.append("\n");
}
System.out.println(sb.toString());
}
//讀取輸入數(shù)據(jù)
private static String readString(int i) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String volume = "";
try {
if (i == 1)
System.out.println("請輸入第" + i + "個長方體的長寬高(請以*分隔每個參數(shù),如:1*2*3):");
else
System.out.println("請輸入第" + i + "個長方體的長寬高:");
do {
volume = countVolume(br.readLine().trim());
if ("".equals(volume)) {
System.out.println("您輸入的參數(shù)錯誤(請以*分隔每個參數(shù),如:1*2*3),請檢查后重新輸入:");
}
} while ("".equals(volume));
volume = "第" + i + "個長方體的體積是:" + volume;
} catch (IOException e) {
e.printStackTrace();
}
return volume;
}
//計算體積
private static String countVolume(String str) {
String[] volumeInfo = str.replaceAll("×", "*").replaceAll("*", "*").split("\\*");
long length = 0;
long width = 0;
long height = 0;
if (volumeInfo.length == 3) {
length = Long.parseLong(volumeInfo[0]);
width = Long.parseLong(volumeInfo[1]);
height = Long.parseLong(volumeInfo[2]);
return String.valueOf((length * width * height));
} else {
return "";
}
}
}
import java.util.Scanner;
public class Box {
int lenght ;
int width ;
int height ;
public int getVolume(int lenght,int width,int height) {
int volume = 0 ;
volume = lenght * width * height ;
return volume ;
}
public static void main(String[] args) {
Box box = new Box() ;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入長:") ;
int lenght = sc.nextInt() ;
System.out.println("請輸入寬:") ;
int width = sc.nextInt() ;
System.out.println("請輸入高:") ;
int height = sc.nextInt() ;
System.out.print("該長方體體積為:"+box.getVolume(lenght, width, height)) ;
}
}
import java.util.Scanner;
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
//主要代碼 System.out.print("長:") double num1=input.nextDouble(); //代碼太多以后簡寫System...("寬:"); double num2=.... System....("高:")double num3=.... System...("表:"+(num1*num2+num1*num3+num2*num3)*2+" 體積:"+n1*n2*n3)//打字不容易求給分