為什么圓的周長(zhǎng)算出來(lái)的小數(shù)點(diǎn)的位數(shù)不對(duì)
package?com.yong; public?abstract?class?Shape?{ public?abstract?void?Perimeter(); public?abstract?void?Area(); }
package com.yong;
public class Rectangle extends Shape {
double a=10;
double b=20;
@Override
public void Perimeter() {
double c=(a+b)*2;
System.out.println("長(zhǎng)方形的周長(zhǎng)為:"+c);
}
@Override
public void Area() {
double s=a*b;
System.out.println("長(zhǎng)方形的面積為:"+s);
}
}
package com.yong;
public class Circle extends Shape {
final double PI=3.14;
double r=10.0;
@Override
public void Perimeter() {
double c=2*PI*r;
System.out.println("圓的周長(zhǎng)為:"+c);
}
@Override
public void Area() {
double s=PI*r*r;
System.out.println("圓的面積為:"+s);
}
}
package com.yong;
public class Test {
public static void main(String[] args) {
Shape sh1=new Rectangle();
sh1.Area();
sh1.Perimeter();
Shape sh2=new Circle();
sh2.Perimeter();
sh2.Area();
}
}
長(zhǎng)方形的面積為:200.0
長(zhǎng)方形的周長(zhǎng)為:60.0
圓的周長(zhǎng)為:62.800000000000004
圓的面積為:314.0
2018-03-31
System.out.println("圓的周長(zhǎng)為:"+c);
System.out.println("圓的面積為:"+s);
這些都能寫(xiě)到父類里面嗎,為什么?求指導(dǎo)
2018-01-25
改為? System.out.println("圓的周長(zhǎng)為:"+String.format("%.1f", c));
2018-01-25
位數(shù)對(duì)
輸入的數(shù)算圓面積和長(zhǎng)方形的周長(zhǎng)面積是個(gè)整數(shù),但是定義的是double型的,所以后面有0補(bǔ)位;但是圓的周長(zhǎng)不是整數(shù),不用補(bǔ)位。