这是完整的代码。 这是我的主要方法..
public static void main(String[] args)
{
float radius;
Scanner in = new Scanner(System.in);
Circle c = new Circle(); // Circle Class object
System.out.print("\nEnter Circle Radius : ");
radius = in.nextFloat();
c.SetRadius(radius); // Passing radius value..
c.display();
}
这是Circle完整的类代码
Class Circle
{
private float radius;
float area , circumference;
float pi = 3.14f;
public void SetRadius(float r)
{
radius = r;
}
float getRadius()
{
return radius;
}
void area()
{
area = pi * ( radius * radius );
}
void Circumference()
{
circumference = 2 * pi * radius;
}
void display()
{
System.out.println("The Area of Circle is : "+area);
System.out.println("The Circumference of Circle is : "+circumference);
}
每件事似乎都是正确的,但我不知道为什么区域变量总是存储0.0值。
答案 0 :(得分:2)
它只存储0 by default。将您的代码放入Main
类,我得到以下结果(完全符合预期):
public static void main(String[] args) {
Main m = new Main();
System.out.println(m.area); //0 by default (printed as 0.0)
m.area();
System.out.println(m.area); //0 by default again (since radius is not yet set)
m.SetRadius(10);
m.area();
System.out.println(m.area); //314.0
}