请教各位大神,最后输出为何是零?
如图。请教一下各位,为何最后输入信息之后,输出的值始终为零呢?
如图。请教一下各位,为何最后输入信息之后,输出的值始终为零呢?
2018-11-07
public abstract class Shape { public double C; public double S; public abstract void Perimeter(); public abstract void Area(); }
public class Circle extends Shape{ double R; final double PI = 3.14; Circle(double RR){ this.R = RR; } public void Perimeter(){ C = 2 * PI * R; System.out.println("圆形的周长为" + C); } public void Area(){ S = PI * R * R; System.out.println("圆形的面积为" + S); } }
public class Rectangle extends Shape{ double l; double d; Rectangle(double ll, double dd){ this.l = ll; this.d = dd; } public void Perimeter(){ C = 2 * (l + d); System.out.println("矩形的周长为" + C); } public void Area(){ S = l * d; System.out.println("矩形的面积为" + S); } }
import java.util.Scanner; public class Test { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("请输入矩形的长和宽"); double l = input.nextDouble(); double d = input.nextDouble(); Shape sp1 = new Rectangle(l, d); sp1.Perimeter(); sp1.Area(); System.out.println("请输入圆形的半径"); double r = input.nextDouble(); Shape sp2 = new Circle(r); sp2.Perimeter(); sp2.Area(); } }
请输入矩形的长和宽
2
2
矩形的周长为8.0
矩形的面积为4.0
请输入圆形的半径
2
圆形的周长为12.56
圆形的面积为12.56
举报