题目:有个游泳池,内圈为圆形游泳池,外圈为圆形人行道,外围还有个围墙,游泳池半径为r1,人行道半径为r2,游泳池的造价为a元每平方米,外圈造价为b元每平方米,围墙造价为c元每平方米,要求用面向对象的编程方法求指导造价问题,造价可由用户输入,abc只是参数
注意看清题目,谢谢大家
注意看清题目,谢谢大家
解决方案
30
您确定不是c元每米?
public class Circle { static final double PI = 3.14; double radius; double perimeter(){ return radius*PI*2; } double area(){ return PI*(Math.pow(radius,2)); } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public static void main(String[] args){ Scanner scan =new Scanner(System.in); System.out.println("输入游泳池每平方米造价"); double a = scan.nextDouble(); //a,游泳池每平方造价 System.out.println("输入人行道每平方米造价"); double b = scan.nextDouble(); //b,外圈每平方造价 System.out.println("输入围墙每米造价"); double c = scan.nextDouble(); //c,围墙每米造价 Circle circle_swimPool = new Circle(); Circle circle_outCircle= new Circle(); circle_swimPool.setRadius(60);//r1暂定为60 circle_outCircle.setRadius(100);//r2暂定为100 double circle_swimPool_area = circle_swimPool.area(); double circle_outCircle_area = circle_outCircle.area(); double circle_avenue_area = circle_outCircle_area-circle_swimPool_area; double circle_outCicle_perimeter = circle_outCircle.perimeter(); double swimpool_area_price = circle_swimPool_area*a; double avenue_area_price = circle_avenue_area*b; double wall_price = circle_outCicle_perimeter*c; System.out.println(swimpool_area_price); System.out.println(avenue_area_price ); System.out.println(wall_price); } }
假如执意要在结果内输出r1,r2。将输出的数据类型改成String,中间要用到toString(但本人觉得意义不大)
还有,为啥放到J2EE的版块?