做一个我认为对AP来说非常容易的输出实验室,我以为我正在做的一切都正确,但是当“0.0”是“0.0”时输出仍然是“区域是::”+ 0.0“应该是圆的计算面积。这是两个类:
圆圈类:
public class Circle
{
private double radius;
private double area;
public void setRadius(double rad)
{
rad = radius;
}
public void calculateArea( )
{
area = (3.14159*(radius*radius));
}
public void print( )
{
System.out.println("The area is :: " + area);
}
}
Circle Runner Class:
public class CircleRunner
{
public static void main( String[] args )
{
Circle test = new Circle ( );
test.setRadius(7.5);
test.calculateArea( );
test.print( );
test.setRadius(10);
test.calculateArea( );
test.print( );
test.setRadius(72.534);
test.calculateArea( );
test.print( );
test.setRadius(55);
test.calculateArea( );
test.print( );
}
}
谢谢!
答案 0 :(得分:4)
你的二传手是错的,所以你的半径保持为0,它应该是
public void setRadius(double rad) {
radius = rad;
}
答案 1 :(得分:1)
您正在以相反的顺序分配变量。它应该是:
radius = rad;
而不是:
rad = radius;
答案 2 :(得分:0)
您需要使用它来引用setRadius方法中的半径。或交换这两个值。你应该分配" rad"到"半径"而不是相反,因为你将从方法中接收rad。
public class Circle {
private double radius;
private double area;
public Circle() {
}
public void setRadius(double rad) {
this.radius = rad;
}
public void calculateArea() {
area = (3.14159*(radius*radius));
}
public void print() {
System.out.println("The area is :: " + area);
}
}