0
/****Setters and Getter****/

public double getx1() {
return x1;//x1 getter
}

public void setx1(double x1) {
this.x1 = x1;//x1 setter
}

public double getx2() {
return x2;//x2 getter
}

public void setx2(double x2) {
this.x2 = x2;//x2 setter
}

public double getx3() {
return x3;//x3 getter
}

public void setx3(double x3) {
this.x3 = x3;//x3 setter
}

public double gety1() {
return y1;//y1 getter
}

public void sety1(double y1) {
this.y1 = y1;//y1 setter
}

public double gety2() {
return y2;//y2 getter
}

public void sety2(double y2) {
this.y2 = y2;//y2 setter
}

public double gety3() {
return y3;//y3 getter
}

public void sety3(double y3) {
this.y3 = y3;//y3 setter
}

/****Splitting existing Array made up by coordinates by comma******/

public void split() {
    String[] Coord1 = point1.split(",");
    String[] Coord2 = point2.split(",");
    String[] Coord3 = point3.split(",");


/****Changing String inputs of the coordinates to integers******/

    double x1 = Integer.parseInt(Coord1[0]);
    double x2 = Integer.parseInt(Coord2[0]);
    double x3 = Integer.parseInt(Coord3[0]);
    double y1 = Integer.parseInt(Coord1[1]);
    double y2 = Integer.parseInt(Coord2[1]);
    double y3 = Integer.parseInt(Coord3[1]);
}


/***MY perimeter calculations***/
public double perimeter(){
    side1 = Math.sqrt(((y1-x1)*(y1-x1))+((y2-x2)*(y2-x2)));
    side2 = Math.sqrt(((y2-x2)*(y2-x2))+((y3-x3)*(y3-x3)));
    side3 = Math.sqrt(((y3-x3)*(y3-x3))+((y1-x1)*(y1-x1)));

    double perimeter = side1+side2+side3;//perimeter formula

    return perimeter;

如何将整数值分配给上面的设置器?我接受了来自用户的坐标输入作为字符串 ex。3,4。然后,我用逗号分割它并将其存储到一个数组中。之后,我解析它以将其值更改为整数而不是字符串,因为我需要对数字进行数学运算。我不确定如何将其整数值分配给设置器,或者我是否应该这样做。

4

1 回答 1

0

您提供的代码的一个问题在于split方法。最后,您对所有变量进行了赋值,但是您使用double x1 = ...了 ,因此您创建了一个名为 的新变量x1,它与this.x1. 然后你永远不会使用这个变量。这让我觉得你打算这样做:

x1 = Integer.parseInt(Coord1[0]);
x2 = Integer.parseInt(Coord2[0]);
x3 = Integer.parseInt(Coord3[0]);
y1 = Integer.parseInt(Coord1[1]);
y2 = Integer.parseInt(Coord2[1]);
y3 = Integer.parseInt(Coord3[1]);

这一次,没有double在分配的开始。

于 2013-10-17T00:19:40.673 回答