Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Java)卡在一个方法上_Java_Math_Methods - Fatal编程技术网

(Java)卡在一个方法上

(Java)卡在一个方法上,java,math,methods,Java,Math,Methods,我很难弄明白怎么做。我要做的是接收一个X和一个Y都是int,并将它们添加到两个端点的X和Y。例如,如果一个端点为(1,2),另一个端点为(5,6),则接收到的数字为(3,3)。最后的端点必须是(4,5)和(8,9)。这是我到目前为止的代码 public class Segment { private Point Point1; private Point Point2; public Segment ( ) { this.Point1 = new Point(0, 0); th

我很难弄明白怎么做。我要做的是接收一个X和一个Y都是int,并将它们添加到两个端点的X和Y。例如,如果一个端点为(1,2),另一个端点为(5,6),则接收到的数字为(3,3)。最后的端点必须是(4,5)和(8,9)。这是我到目前为止的代码

public class Segment
{
private Point Point1;
private Point Point2;

public Segment ( )
{
    this.Point1 = new Point(0, 0);
    this.Point2 = new Point(7, 7);
}

public Segment (int x1, int y1, int x2, int y2)
{
    if (x1 == x2 && y1 == y2)
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = new Point (x1,y1);
            Point2 = new Point (x2,y2);
        }
}

public Segment (Point p1, Point p2)
{
    if (p1 == p2)
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = new Point (p1);
            Point2 = new Point (p2);
        }
}

public Segment (Segment other)
{
    Point1 = new Point (other.Point1);
    Point2 = new Point (other.Point2);
}
public String toString ( )
{
    return (Point1) + "---------" + (Point2);
}

//More codes

public void translate (int xmove, int ymove) // <--- Need help here.
{
    this.Point1 = ((x1 + xmove), (y1 + ymove));
    this.Point2 = this.Point2 + ymove;
}
公共类段
{
专用点1;
专用点2;
公共部分()
{
this.Point1=新点(0,0);
this.Point2=新的点(7,7);
}
公共段(整数x1、整数y1、整数x2、整数y2)
{
如果(x1==x2&&y1==y2)
抛出新的IllegalArgumentException(“错误,这将导致段为0。请重新输入两个不同的值。”);
其他的
{
点1=新点(x1,y1);
点2=新点(x2,y2);
}
}
公共段(p1点、p2点)
{
如果(p1==p2)
抛出新的IllegalArgumentException(“错误,这将导致段为0。请重新输入两个不同的值。”);
其他的
{
点1=新点(p1);
点2=新点(p2);
}
}
公共部分(其他部分)
{
点1=新点(其他点1);
点2=新点(其他点2);
}
公共字符串toString()
{
返回(点1)+“------------”+(点2);
}
//更多代码
public void translate(int-xmove,int-ymove)//尝试以下操作:

public void translate (int xmove, int ymove)
{
    this.Point1.x += xmove;
    this.Point1.y += ymove;
    this.Point2.x += xmove;
    this.Point2.y += ymove;
}
此外,正如其他人所说,在比较两点时,您需要使用
.equals
而不是
=
。此外,您还需要正确分配点,这里我已经稍微整理了您的代码:

public class Segment
{
private Point Point1;
private Point Point2;

public Segment ( )
{
    this.Point1 = new Point(0, 0);
    this.Point2 = new Point(7, 7);
}

public Segment (int x1, int y1, int x2, int y2)
{
    if (x1.equals(x2) && y1.equals(y2))
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = new Point (x1,y1);
            Point2 = new Point (x2,y2);
        }
}

public Segment (Point p1, Point p2)
{
    if (p1.equals(p2))
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = p1;
            Point2 = p2;
        }
}

public Segment (Segment other)
{
    Point1 = other.Point1;
    Point2 = other.Point2;
}
public String toString ( )
{
    return (Point1) + "---------" + (Point2);
}

//More codes

public void translate (int xmove, int ymove)
{
    this.Point1.x += xmove;
    this.Point1.y += ymove;
    this.Point2.x += xmove;
    this.Point2.y += ymove;
}
试试这个:

public void translate (int xmove, int ymove)
{
    this.Point1.x += xmove;
    this.Point1.y += ymove;
    this.Point2.x += xmove;
    this.Point2.y += ymove;
}
此外,正如其他人所说,在比较两点时,您需要使用
.equals
而不是
=
。此外,您还需要正确分配点,这里我已经稍微整理了您的代码:

public class Segment
{
private Point Point1;
private Point Point2;

public Segment ( )
{
    this.Point1 = new Point(0, 0);
    this.Point2 = new Point(7, 7);
}

public Segment (int x1, int y1, int x2, int y2)
{
    if (x1.equals(x2) && y1.equals(y2))
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = new Point (x1,y1);
            Point2 = new Point (x2,y2);
        }
}

public Segment (Point p1, Point p2)
{
    if (p1.equals(p2))
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = p1;
            Point2 = p2;
        }
}

public Segment (Segment other)
{
    Point1 = other.Point1;
    Point2 = other.Point2;
}
public String toString ( )
{
    return (Point1) + "---------" + (Point2);
}

//More codes

public void translate (int xmove, int ymove)
{
    this.Point1.x += xmove;
    this.Point1.y += ymove;
    this.Point2.x += xmove;
    this.Point2.y += ymove;
}

您可以在名为
incrementPoint(intx,inty)
类中创建一个方法:

然后,在
translate
方法中,只需执行以下操作:

public void translate (int xmove, int ymove) {
    this.point1.incrementPoint(xmove, ymove);
    this.point2.incrementPoint(xmove, ymove);
}

不要使用大写字母来开始类属性的名称,而是使用标准。

您可以在名为
incrementPoint(int x,int y)
类中创建一个方法:

然后,在
translate
方法中,只需执行以下操作:

public void translate (int xmove, int ymove) {
    this.point1.incrementPoint(xmove, ymove);
    this.point2.incrementPoint(xmove, ymove);
}

不要使用大写字母来开始类属性的名称,而是使用标准。

似乎您正在向
点1
点2
添加
xmove
ymove

我假设Point类由int x和y组成,它们具有公共可见性

public void translate (int xmove, int ymove) {
  this.Point1.x += xmove; this.Point2.y += ymove;
  this.Point2.x += xmove; this.Point2.y += ymove;
}
假设Point类的可见性有限

public void translate (int xmove, int ymove) {
  this.Point1.x += xmove; this.Point2.y += ymove;
  this.Point2.x += xmove; this.Point2.y += ymove;
}
如果Point类没有setter,只有getter

public void translate (int xmove, int ymove) {
  this.Point1 = new Point(this.Point1.getX() + xmove, this.Point1.getY() + ymove);
  this.Point2 = new Point(this.Point2.getX() + xmove, this.Point2.getY() + ymove);
}
If Point类同时具有getter和setter

public void translate (int xmove, int ymove) {
  this.Point1.setX(Point1.getX() + xmove); this.Point1.setY(Point1.getY() + ymove);
  this.Point2.setX(Point2.getX() + xmove); this.Point2.setY(Point2.getY() + ymove);
}

另外,请注意,不建议以大写字母开头的变量命名,如Point1和Point2变量。这会混淆类名和变量名。这种做法的例外情况是常量值,如
最终静态点原点
,其中所有字母通常都是大写的。

您正在将
xmove
ymove
添加到
Point1
Point2

public void translate (int xmove, int ymove) // <--- Need help here.
{
    this.Point1 = new Point((this.Point1.x + xmove), (this.Point1.y + ymove));
    this.Point2 = new Point((this.Point2.x + xmove), (this.Point2.y + ymove));
}
我假设Point类由int x和y组成,它们具有公共可见性

public void translate (int xmove, int ymove) {
  this.Point1.x += xmove; this.Point2.y += ymove;
  this.Point2.x += xmove; this.Point2.y += ymove;
}
假设Point类的可见性有限

public void translate (int xmove, int ymove) {
  this.Point1.x += xmove; this.Point2.y += ymove;
  this.Point2.x += xmove; this.Point2.y += ymove;
}
如果Point类没有setter,只有getter

public void translate (int xmove, int ymove) {
  this.Point1 = new Point(this.Point1.getX() + xmove, this.Point1.getY() + ymove);
  this.Point2 = new Point(this.Point2.getX() + xmove, this.Point2.getY() + ymove);
}
If Point类同时具有getter和setter

public void translate (int xmove, int ymove) {
  this.Point1.setX(Point1.getX() + xmove); this.Point1.setY(Point1.getY() + ymove);
  this.Point2.setX(Point2.getX() + xmove); this.Point2.setY(Point2.getY() + ymove);
}

另外,请注意,不建议以大写字母开头的变量命名,如Point1和Point2变量。这会混淆类名和变量名。这种做法的例外情况是常量值,如
最终静态点原点
,其中所有字母通常大写。

publicvoid translate(int-xmove,int-ymove)//
public void translate(int-xmove,int-ymove)//您所要求的是在“translate”方法中发生的吗?是的,这是正确的。您可能希望使用
p1.equals(p2)
(并正确地覆盖
点中的
equals
hashCode
),而不是使用
=
来比较它们。Java不允许运算符重载,因此
=
比较内存地址,而不是对象内容。谢谢@AndyTurner,我一直忘了使用。对于类,等于。您要求的是“translate”方法吗?是的,这是正确的。您可能想使用
p1.equals(p2)
(并在
中正确重写
equals
hashCode
),而不是使用
=
来比较它们。Java不允许运算符重载,所以
=
比较内存地址,而不是对象内容。谢谢@AndyTurner我一直忘记使用。类使用equals。
public void translate (int xmove, int ymove) // <--- Need help here.
{
    this.Point1 = new Point((this.Point1.x + xmove), (this.Point1.y + ymove));
    this.Point2 = new Point((this.Point2.x + xmove), (this.Point2.y + ymove));
}