设计计算长方体体积的java
发布网友
发布时间:2023-07-14 20:39
我来回答
共4个回答
热心网友
时间:2024-12-02 18:00
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)) ;
}
}
热心网友
时间:2024-12-02 18:01
import java.util.Scanner;
class cuboid
{
private float length;
private float width;
private float height;
public cuboid(float a,float b,float c)//构造函数初始化
{
length=a;
width=b;
height=c;
}
public float getLength()
{
return length;
}
public float getWidth()
{
return width;
}
public float getHeight()
{
return height;
}
/**calculate the volume of the cuboid(计算体积)
* */
public float Volume_cuboid()//计算体积并返回
{
return length*width*height;
}
public void print() //打印体积的结果
{
System.out.println("The volume is: "+Volume_cuboid());
}
}
public class volume {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int length;
int width;
int height;
System.out.println("input length,width and height:");
length=sc.nextInt();
width=sc.nextInt();
height=sc.nextInt();
cuboid abc=new cuboid(length,width,height);//实例化类cuboid为对象abc
abc.print();//调用打印函数
}
}
热心网友
时间:2024-12-02 18:01
import java.util.Scanner;
public class Test33
{
public static void main(String [] args)
{
Scanner reader=new Scanner(System.in);
System.out.println("Plz enter length:");
int length=reader.nextInt();
System.out.println("Plz enter width:");
int width=reader.nextInt();
System.out.println("Plz enter heigth:");
int height=reader.nextInt();
Box a=new Box();
a.volume(length,width,height);
}
}
class Box
{
int length,width,height,vo;
public void volume(int length,int width,int height)
{
this.length=length;
this.width=width;
this.height=height;
vo=length*width*height;
System.out.println(vo);
}
}
热心网友
时间:2024-12-02 18:02
int length;
int width;
int height;
int v = length * width * height