我的支持班
/*
*Calculates Perimeter of three triangles
*/
public class Triangle{
//data field declarations
public int x1; // coordinates of first point of the triangle
public int y1;
public int x2; // coordinates of second point of the triangle
public int y2;
public int x3; // coordinates of third point of the triangle
public int y3;
private double side1, side2, side3, perimeter;
private String m;
public Triangle(String m, int x1, int y1, int x2, int y2, int x3, int y3)
{
this.m = m;
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}
public void calculateSideLength()
{
side1 = Math.sqrt(Math.pow((x2-x1),2)*Math.pow((y2-y1),2));
side2 = Math.sqrt(Math.pow((x3-x2),2)*Math.pow((y3-y2),2));
side3 = Math.sqrt(Math.pow((x3-x1),2)*Math.pow((y3-y1),2));
}
public void calculateTrianglePerimeter()
{
perimeter = side1+side2+side3;
}
public String toString()
{
return "Triangle " + m + " perimeter is " + perimeter + " units";
}
}
我的申请班
/*
*Calculates Perimeter of three triangles
*/
public class TriangleApp{
public static void main (String[] args){
Triangle a = new Triangle("A", 0,3,3,4,1,9);
System.out.println(a.toString());
}
}
我得到三角形的周长是0.0单位
有人可以告诉我我在做什么错吗?
最佳答案
您不是在呼叫calculateSideLength()
或calculateTrianglePerimeter()
。调用它们(从构造函数,该顺序或从用户代码),然后将设置值。
关于java - 我正在尝试编写代码以计算周长,但结果是0.0单位,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7426446/