为什么我圆形的面积后面保留了那么多位?
package com.imooc.modle;
public abstract class Shape {
public abstract double area();
}
package com.imooc.modle;
public class Rectangle extends Shape {
private double length;
private double wide;
public Rectangle() {
}
public Rectangle(double length, double wide) {
this.setLength(length);
this.setWide(wide);
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWide() {
return wide;
}
public void setWide(double wide) {
this.wide = wide;
}
public double area() {
// TODO Auto-generated method stub
double area=this.getLength()*this.getWide();
return area;
}
}
package com.imooc.modle;
public class Circle extends Shape{
private double r;
public Circle() {
}
public Circle(double r) {
this.setR(r);
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double area() {
// TODO Auto-generated method stub
double area=this.getR()*this.getR()*Math.PI;
return area;
}
}
package com.imooc.test;
import com.imooc.modle.Circle;
import com.imooc.modle.Rectangle;
import com.imooc.modle.Shape;
public class Test1 {
public static void main(String[] args) {
Shape one=new Rectangle(5, 6);
Shape two=new Circle(4);
System.out.println(one.area());
System.out.println(two.area());
}
}
正在回答
默认就是会保留小数位的!除非你调用方法,例如四舍五入方法或者自己处理,去保留小数后几位。祝:学习愉快
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星