JAVA creates a Box class (cuboid), and defines three variables in the Box class, respectively representing the length (length), width (width) and height (heigth) of the cuboid

Program to create a Box class (cuboid), define three variables in the Box class, respectively representing the length (length), width (width) and height (heigth) of the cuboid, and then define a method void setBox(int ​​l, int w, int h) Initialize these three variables, and then define a method int volume () to calculate the volume of the cuboid. Finally, create an object b of the Box class in the main() method, first set the length, width and height of the cuboid by calling the setBox() method of the object b, and then calculate the given value by calling the volume() method of the object b The volume of the sized cuboid and print the result.

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();
       System.out.print("宽:");
		int w = input.nextInt();
       System.out.print("高:");
		int h =input.nextInt();
		b.setBox(l, w, h);
		System.out.println("体积为:" + b.volume());
		
	}

}




Guess you like

Origin blog.csdn.net/qq_43952288/article/details/106926331