我正在尝试将对象中的一些数据输出到控制台。似乎工作正常,但在数据之下,我收到了一条奇怪的信息。
以下是代码:
import java.text.DecimalFormat;
/**
* The Circle class calculates area, diameter, and circumference
*/
public class Circle {
DecimalFormat fmt = new DecimalFormat("#0.00");
// Fields
private double radius; // Holds the circle radius
private final double PI = 3.14159; // The formula for PI
/**
* Start Constructors
*/
public Circle(double rad) {
radius = rad;
System.out.println("The circle's radius is " + (fmt.format(radius)));
System.out.println("The circle's area is " + (fmt.format(PI * radius * radius)));
System.out.println("The circle's diameter is " + (fmt.format(radius * 2)));
System.out.println("The circle's circumference is " + (fmt.format(2 * PI * radius)));
}
}
控制台输出是:
The circle's radius is 2.00
The circle's area is 12.57
The circle's diameter is 4.00
The circle's circumference is 12.57
Circle@5115a298
什么是圈子消息?
答案 0 :(得分:1)
为您的Circle类提供合适的toString()
方法覆盖。您将看到Object的此方法的默认版本返回。
@Override
public String toString() {
/* return a String that shows the "state" of this object */
}
答案 1 :(得分:1)
您必须在其他地方只有一个Circle对象作为输出。然后Hovercraft Full Of Eels的回答适用。
顺便说一句:Math.PI已经给你了Pi!