搞了一个小时,还是不能返回值。高手指点一下啊。

celltree001 2007-04-12 11:01:59
题目如下:
实现一个类,描述二维平面的点Point。
描述属性:私有属性
横坐标:double x
纵坐标:double y
实现方法:
二维点的构建;
返回点的横坐标;
返回点的纵坐标;
显示坐标信息,各式:“(x,y)”;
translate(double dx,double dy),移动点:向右移动dx单位,向上移动dy单位;
ratate(double ta),将点沿逆时针方向旋转ta角度
x2 = x1*cos(ta) - y1*sin(ta)
y2 = x1*sin(ta) + y1*cos(ta)
distance(p2)返回该点与点p2的距离;
</b>
下面是我写的程序:
import java.io.*;
import java.math.*;
class Point
{ private double x; //x
private double y;// y

void Point(double a,double b)
{x=a;
y=b;
}
public double getX()//
{ return x;
}
public double getY(){
return y;
}//

public void translate(double dx,double dy)
{x=x+dx;
y=y+dy;
}
public void ratate(double ta)
{ x = x*Math.cos(ta) - y*Math.sin(ta);
y = x*Math.sin(ta) + y*Math.cos(ta);

}
public double distance(Point p2)
{ double temp=Math.pow((x-p2.getX()),2)+Math.pow((y-p2.getY()),2);
return Math.sqrt(temp);

}
void display()
{System.out.println("the x is :"+x);
System.out.println("the y is :"+y);
System.out.println("the number is "+"("+x+","+y+")");
}
public static void main(String [] args)
throws java.io.IOException
{String s1,s2,s3,s4,s5,s6,s7;
double a,b,dx,dy,ta,x2,y2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of x:");
s1=br.readLine();
a=Double.parseDouble(s1);
System.out.println("Enter the value of y:");
s2=br.readLine();
b=Double.parseDouble(s2);
Point p1=new Point();
p1.Point(a,b);
p1.display();
System.out.println("Enter the value of move !");
System.out.println("Enter x:");
s3=br.readLine();
System.out.println("Enter y:");
s4=br.readLine();
dx=Double.parseDouble(s3);
dy=Double.parseDouble(s4);
p1.translate(dx,dy);
p1.display();
System.out.println("Enter the radius :");
s5=br.readLine();
ta=Double.parseDouble(s5);
p1.ratate(ta);
p1.display();
System.out.println("Enter the another point !");
System.out.println("Enter the x:");
s6=br.readLine();
System.out.println("Enter the y:");
s7=br.readLine();
x2=Double.parseDouble(s6);
y2=Double.parseDouble(s7);
Point p2=new Point();
p2.Point(x2,y2);
p1.distance(p2);

}

}
运行正常,可是就是不能返回最后一个p1,p2点的距离,怎么回事啊?
...全文
280 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
约翰羊 2007-04-13
  • 打赏
  • 举报
回复
怎么不能返回?
我试过,方法没有问题,也能返回数据.
是数据不显示还是数据不对?

昨天你在天涯上就没写清楚,今天还没有写清楚.^_^
maquan 2007-04-13
  • 打赏
  • 举报
回复
不是“不能返回”,是你“没把返回值打印出来”
malligator 2007-04-13
  • 打赏
  • 举报
回复
p1.distance(p2);

->
System.out.println(p1.distance(p2));
celltree001 2007-04-13
  • 打赏
  • 举报
回复
有一个返回两个点的距离的方法,不能返回他们之间的距离,运行是正常的。但是不能返回他们的两点的距离。
insiku 2007-04-12
  • 打赏
  • 举报
回复
Enter the value of x:
1
Enter the value of y:
1
the x is :1.0
the y is :1.0
the number is (1.0,1.0)
Enter the value of move !
Enter x:
2
Enter y:
2
the x is :3.0
the y is :3.0
the number is (3.0,3.0)
Enter the radius :
3
the x is :-3.393337513980938
the y is :-3.4488453071241763
the number is (-3.393337513980938,-3.4488453071241763)

正常
jk88811 2007-04-12
  • 打赏
  • 举报
回复
太长了...

能说下具体哪里有问题吗?

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧