设计一个圆类,提供有参无参两个构造方法来初始化类的成员变量,提供圆面积的功能;在主函数中产生两个对象实例,从键盘流得到的数据作为圆的半径值,分别调用有参无参构造方法,在控制台输出两个对象的面积。
import java.util.Scanner;
public class Circles(){
private double r;
final static double PI=3.14;
public Circles(double r){
this.r=r;}
public void setR(double r){
this.r=r;}
public double getR(){
return r;
}
Circles(){}
public double getArea(){
return PI*r*r;}
public static void main(String[] args){
Scanner s1=new Scanner(System.in);
System.out.print("r1=");
Circles n1=new Circles(s1.nextDouble());
System.out.println("S1="+n1.getArea());
Scanner s2=new Scanner(System.in);
System.out.print("r2=");
Circles n2=new Circles(s2.nextDouble(r2));
System.out.println("S2="+n2.getArea());
}
}
有一个错
Jaydon_
笑靥无伤
相关分类