创建一个Box类(长方体),在Box类中定义三个变量,length、width、height.......

编程创建一个 Box 类(长方体),在 Box 类中定义三个变量,分别表示长方体的长
(length)、宽(width)和高(heigth),再定义一个方法 void setBox(int l, int w, int h) 对这
三个变量进行初始化,然后定义一个方法 int volume ()来计算长方体的体积。最后,
在 main()方法中创建一个 Box 类的对象 b,首先通过调用对象 b 的 setBox()方法来设
置长方体的长、宽和高,再通过调用对象 b 的 volume() 方法来计算这个给定尺寸的长
方体的体积,并输出这个结果。

/**
 *  使用方法计算长方体体积
 * @author Monster丶ZF
 * @version1.8
 * @data 2019年4月12日
 * @remakeTODO
 */ 

package snippet;

import java.util.Scanner;
	public class Box{
		int length ;
		int width ;
		int height ;
	
	public  void setBox(int l,int w,int h){
		 length = l;
		 width =  w;
		 height = h;
	}
	public  int volume(){
	
	  return length * width * height;
	}

	public static void main(String[] args) {
		Box b = new Box();
		Scanner input = new Scanner(System.in);
		System.out.print("请输入长宽高:");
		int l = input.nextInt();
		int w = input.nextInt();
		int h =input.nextInt();
		b.setBox(l, w, h);
		System.out.println("长方体的体积为:" + b.volume());
		input.close();
		
	}

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/w15977858408/article/details/89252512