用JAVA编写一个计算立方体、球体和圆柱的体积

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JAVA编写一个计算立方体、球体和圆柱的体积相关的知识,希望对你有一定的参考价值。

参考技术A class A//类A计算立方体体积 class B//类A计算球体体积 class C//类A计算圆柱体积 //主类public class test static System.out.println("请输入1、2、3对应立方体、球体和圆柱的体积计算..."); public static void main(String args[]) if(args.length>1 || Integer.parseint(args[0])<1|| Integer.parseint(args[0])>3)//判断输入是否符合标准 System.out.println("错误的选择,程序自动退出.."); System.exit(1); else int x = Integer.parseint(args[0]) ; switch(x) case 1: //调用A类 case 2: //调用B类 defualt: //调用C类 参考技术B package com.answer.dataStrutures;import java.math.*;public class BulkAccount

String figure; //形状
double r;
double h; public BulkAccount(String figure, double r, double h) //对变量的初始化

this.figure = figure;
this.h = h;
this.r = r;
public double bulkAccount(String figure, double r, double h)

double result = 0;
if (figure.trim().equals("cube")) //立方体体积计算

result = r * r * r;

if (figure.trim().equals("cylinder")) //圆柱体体积计算

result = Math.PI * r * r * h;

if (figure.trim().equals("ball")) //球体积计算

result = (4 / 3) * Math.PI * r * r * r;
return result;
public static void main(String[] args)

/*BulkAccount ba = new BulkAccount("cube",2,0);
System.out.println(ba.bulkAccount(ba.figure, ba.r, ba.h));*/
没用测数据了,意思写了你看看吧。
参考技术C 这个题目挺简单的,主要考察方法的重载;先去了解重载的定义。下面是我做的..希望你能接纳!package com.softeem.TZ;public class caculateVl
final double PI = 3.14;
double cubeLengh;
double cubeWigth;
double cubeHigh;
double cubeVl;

double sphereRaius;
double sphereVl;

double columnRaius;
double columnHigh;
double columnVl;

// 3/4 Pi rRR
public void caculateVl(double cubeLengh, double cubeWigth, double cubeHigh)
this.cubeLengh = cubeLengh;
this.cubeWigth = cubeWigth;
this.cubeHigh = cubeHigh;


public void caculateVl(double sphereRaius)
this.sphereRaius = sphereRaius;
public void caculateVl(double columnRaius, double columnHigh)
this.columnRaius = columnRaius;
this.columnHigh = columnHigh;


public void caculateCube(double cubeLengh, double cubeWigth, double cubeHigh)
cubeVl = cubeLengh * cubeWigth * cubeHigh;
System.out.println("立方体面积 :"+cubeVl);


public void caculateSphere(double sphereRaius)
sphereVl = PI * sphereRaius * sphereRaius *sphereRaius*3/4;
System.out.println("球体面积 :"+sphereVl);


public void caculateColumn(double columnRaius, double columnHigh)
columnVl = Math.PI * columnRaius * columnRaius*columnHigh;
System.out.println("圆柱体面积 :"+columnVl);
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
caculateVl CV = new caculateVl();
CV.caculateCube(cubeLengh, cubeWigth, cubeHigh)//括号里面输入值就可以了。
CV.caculateSphere(sphereRaius)
CV.caculateColumn(columnRaius, columnHigh)

参考技术D 具体一点行不行?都给定什么参数,比如给定了高什么之类的 第5个回答  2013-11-15 有测试数据!

/*
* Geometry.java
*/

public class Geometry
public static int instance = 0;
public Geometry ()
instance++;

public static double calculateRectangle(double width, double height)
return width * height;


public static double calculateCube(double width, double height, double length)
return width * height * length;


public static double calculateSphere (double radius)
return (4 * Math.PI * radius * radius * radius) /3;


public static void main(String[] args)
System.out.println(" 1 " + calculateRectangle(4, 5));
System.out.println(" Instance = " + instance);
System.out.println(" 2 " + calculateCube(4, 5, 6));
System.out.println(" Instance = " + instance);
System.out.println(" 3 " + calculateSphere(3));
System.out.println(" Instance = " + instance);

java 用接口计算圆柱的体积和面积并添加颜色

import java.util.Scanner;
class Test {
    public static void main(String[] args) {
        fangfa i = new fangfa();
        Cylinder s1 = new Cylinder();
        Cylinder s2 = new Cylinder();
        Cylinder s3 = new Cylinder();

        System.out.print("容积:");
        i.shixian(s1, 5.0, 6.0);
        System.out.print("面积:");
        i.shixian1(s2, 5.0);
        System.out.println("你想要什么颜色的容器?");
        Scanner scanner = new Scanner(System.in);
        //nextine中的L注意大写
        String str1 = scanner.nextLine();
        String color = str1;
        while (true) {
            if (str1.equals(null)) {
                System.out.println("重新输入");
                continue;
            } else {

                System.out.print("颜色:");
                //将str变量传给字符串,不需要用双引号将变量包起来
                i.shixian2(s3, str1);
                break;
            }

        }

    }
}
class Cylinder implements C{
    public double volume(double radius,double height) {
        return radius * radius * pi * height;
    }
    public double area(double radius1){
        return radius1*radius1*pi;
    }
    public String setcolor(String color){
        return color;
    }


}
class fangfa{
    public double shixian(C per,double radius,double height){
        System.out.println(per.volume(radius,height));
        return 1;

    }
    public double shixian1(C per1,double radius1){
        System.out.println(per1.area(radius1));
        return 2;
    }
    public String shixian2(C per2,String color){
        System.out.println(per2.setcolor(color));
        return "hehe";
    }
}
interface A{
    public static double pi=3.14;
     double area(double radius1);

}
interface B{
    String setcolor(String color);

}

interface C extends A,B{
    double volume(double radius,double height);

}

运行结果:

 

以上是关于用JAVA编写一个计算立方体、球体和圆柱的体积的主要内容,如果未能解决你的问题,请参考以下文章

webgl想要做六个球体在立方体的六个面上

计算圆柱的底面积和体积

项目三第一题

java 用接口计算圆柱的体积和面积并添加颜色

c语言:求圆周长圆面积圆球表面积圆球体积圆柱体积。

如何用Unity制作创建圆柱体模块