java长方体体积代码,长方形面积java

java求3个长方体的体积

随便写了下,希望对你有帮助!

创新互联专注于泗洪企业网站建设,成都响应式网站建设,商城网站建设。泗洪网站建设公司,为泗洪等地区提供建站服务。全流程按需定制开发,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

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());

}

//读取输入数据

private static String readString(int i) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String volume = "";

try {

if (i == 1)

System.out.println("请输入第" + i + "个长方体的长宽高(请以*分隔每个参数,如:1*2*3):");

else

System.out.println("请输入第" + i + "个长方体的长宽高:");

do {

volume = countVolume(br.readLine().trim());

if ("".equals(volume)) {

System.out.println("您输入的参数错误(请以*分隔每个参数,如: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 "";

}

}

}

设计计算长方体体积的java

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)) ;

}

}

用java写一个程序,要求输入3个长方体的长宽高 并分别求出面积和表体积

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

java怎么编写求长方形表面积和体积的代码

//主要代码 System.out.print("长:") double num1=input.nextDouble(); //代码太多以后简写System...("宽:"); double num2=.... System....("高:")double num3=.... System...("表:"+(num1*num2+num1*num3+num2*num3)*2+" 体积:"+n1*n2*n3)//打字不容易求给分


当前文章:java长方体体积代码,长方形面积java
链接地址:http://azwzsj.com/article/hsshjc.html