package com.tmgps.test;
创新互联公司于2013年开始,是专业互联网技术服务公司,拥有项目网站制作、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元富裕做网站,已为上家服务,为富裕各地企业和个人服务,联系电话:18982081108
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入长方体的长");
int a=input.nextInt();
System.out.println("请输入长方体的宽");
int b=input.nextInt();
System.out.println("请输入长方体的高");
int c=input.nextInt();
System.out.println("长方体的体积为:"+a*b*c);
}
}
长宽高都可以自己输入
//主要代码 System.out.print("长:") double num1=input.nextDouble(); //代码太多以后简写System...("宽:"); double num2=.... System....("高:")double num3=.... System...("表:"+(num1*num2+num1*num3+num2*num3)*2+" 体积:"+n1*n2*n3)//打字不容易求给分
代码:
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入圆柱体的底半径:");
int r = scanner.nextInt();
System.out.println("请输入圆柱体的高:");
int h = scanner.nextInt();
double pi = Math.PI;
// 取整数部分
int v = (int) pi * r * r * h;
System.out.println("圆柱体的体积是:" + v);
}
}
效果:
在main方法中实现Shape的对象时,使用Trangle或者Rectangle分别进行实例化。这样对于同一个Pillar的对象,可以调用两个不同类的实现方法来计算。具体代码请参考下面。
Shape.java
public abstract class Shape {
public int dim1;
public int dim2;
public abstract int getArea();
}
Trangle.java
public class Trangle extends Shape {
public int getArea() {
return (super.dim1 * super.dim2) / 2;
}
}
Rectangle.java
public class Rectangle extends Shape {
public int getArea() {
return super.dim1 * super.dim2;
}
}
Pillar.java
public class Pillar {
public Shape shape;
public int height;
public int getVolumn() {
return this.shape.getArea() * this.height;
}
public static void main(String[] args) {
// 三棱柱
Pillar p = new Pillar();
p.shape = new Trangle();
p.shape.dim1 = 2;
p.shape.dim2 = 4;
p.height = 3;
System.out.println("三棱柱的体积是:" + p.getVolumn());
// 四棱柱
p.shape = new Rectangle();
p.shape.dim1 = 2;
p.shape.dim2 = 4;
p.height = 3;
System.out.println("四棱柱的体积是:" + p.getVolumn());
}
}
下面是执行后的结果:
import java.util.Scanner;
/**
* 计算球的体积
*
* @author young
*
*/
public class Volume {
public static void main(String[] args) {
System.out.print("请输入r:");
Scanner reader = new Scanner(System.in);
double r = 0, v = 0;
r = reader.nextDouble();
v = 4 * 3.14159 / 3 * r * r * r;
System.out.println("球体积为:" + String.format("%.2f", v));
}
}