首 页 行业热点 新车 试驾评测 养车用车 车型库

用JAVA语言描述一个盒子类Box,其有长宽高三个属性,并能够设置每一个盒子...

发布网友

我来回答

2个回答

热心网友

import java.util.Scanner;

/**
* BOX
* @author Administrator
*
*/
class Box{
/**
* 长
*/
private int longer;
/**
* 宽
*/
private int wider;
/**
* 高
*/
private int high;

/**
* 计算体积
* @param longer
* @param wider
* @param high
* @return
*/
public int getVolume(int longer, int wider, int high){
return longer * wider * high;
}

public int getLonger() {
return longer;
}
public void setLonger(int longer) {
this.longer = longer;
}
public int getWider() {
return wider;
}
public void setWider(int wider) {
this.wider = wider;
}
public int getHigh() {
return high;
}
public void setHigh(int high) {
this.high = high;
}

}
public class Test{
public static void main(String[] args) {
Box box = new Box();
// 从控制输入参数
Scanner sc = new Scanner(System.in);
System.out.println("请输入BOX的长:");
box.setLonger(sc.nextInt());
System.out.println("请输入BOX的宽:");
box.setWider(sc.nextInt());
System.out.println("请输入BOX的高:");
box.setHigh(sc.nextInt());

System.out.println("请输入BOX的长:" + box.getLonger() + ",宽:" + box.getWider() + ",高:" + box.getHigh());

System.out.println("计算BOX的体积为长*宽*高:" + box.getVolume(box.getLonger(), box.getWider(), box.getHigh()));

}
}
以上是我的个人理解,希望能够帮助你。追问谢谢你 真的

热心网友

class Demo{

public static void main(String[] args) {
Box box =new Box(2, 2, 2);
System.out.println("这个长方体的体积是:" +box.volume());
}

}

class Box {
double width;
double height;
double depth;

Box(double w,double h,double d) {
width = w;
height = h;
depth = d;
}

double volume() {
return width * height * depth;
}
}追问谢谢哈 下面的大哥 有点难看懂 我是小白 呵呵 你有扣扣吗 加我嘛 不懂可以问你

追答qq:tailsfly@qq.com (主显账号设置的为邮箱格式,直接输入就好)

其实楼下写的更全面,我是写的简单点的,但是功能都差不多。
楼下会有用户输入的过程,而我的只能在代码里面预定好数值,所以实际楼下写的比我好。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com