import java.util.*;
class Point {
double x,y;
Point(double a, double b){
x = a;
y = b;
}
double s = Math.sqrt(x*x+y*y); } public class Pointshow{
public static void main(String[] args) {
Point d[]=new Point[10];
for(int i=0;i<10;i++){
d[i].x=Math.random();
d[i].y=Math.random();
}
Arrays.sort(d);
for(int i=0;i<10;i++){
System.out.println("点的坐标是:("+d[i].x+","+d[i].y+")"+"点到原点的距离是:"+d[i].s);
}
// TODO Auto-generated method stub }
}
class Point {
double x,y;
Point(double a, double b){
x = a;
y = b;
}
double s = Math.sqrt(x*x+y*y); } public class Pointshow{
public static void main(String[] args) {
Point d[]=new Point[10];
for(int i=0;i<10;i++){
d[i].x=Math.random();
d[i].y=Math.random();
}
Arrays.sort(d);
for(int i=0;i<10;i++){
System.out.println("点的坐标是:("+d[i].x+","+d[i].y+")"+"点到原点的距离是:"+d[i].s);
}
// TODO Auto-generated method stub }
}
解决方案 »
- "The resource is not on the build path of a java project"
- JAVA 关于四舍五入的问题
- 在java中有没有产生固定尺寸图片的方法,就是在传入图片时就设置图片的大小,而不是往组件上画时设置大小
- socket的问题~~谁能搞定
- 请高手指点一小段排序程序
- 难道java构造函数的参数不能使用对象吗?急待解决,谢谢你的参与!
- Java 存取图片到Sybase image 字段 的问题
- 请教一段关于网络IP的程序
- 到底错在哪? 在线等待~~~急
- 送分问题(20分)
- 初学正则表达式,问个入门的问题
- 如何将FlowLayout布局添加到BorderLayout 的SOUTH?
at Pointshow.main(Pointshow.java:16)
然后调用 Arrays.sort(d,comparator);
import java.util.*;
class Point {
double x,y;
Point(double a, double b){
x = a;
y = b;
}
public double getDistance() // 这里最好用方法,别直接计算,当然你直接算也是可以的
{
return Math.sqrt(x*x+y*y);
} }
class myCoparator implements Comparator //实现Comparator接口
{
public int compare(Object x1,Object x2)
{
Point pb1=(Point)x1;
Point pb2=(Point)x2;
double d1=pb1.getDistance();
double d2=pb2.getDistance();
if( d1>d2) return 1;
if(d1==d2) return 0;
return -1;
}
}
public class Pointshow{
public static void main(String[] args) {
Point d[]=new Point[10];
for( int i=0;i<10;i++)
{
d[i]=new Point(Math.random(),Math.random()); //这是你显示空指针的原因
}
myCoparator mc=new myCoparator();
Arrays.sort(d,mc);
for(int i=0;i<10;i++){
System.out.println("点的坐标是:("+d[i].x+","+d[i].y+")"+"点到原点的距离是:"+d[i].getDistance());
}
// TODO Auto-generated method stub }
}
for(int i=0;i<10;i++)
point[i]=new Point(Math.random(),Math.random());
at Pointshow.main(Pointshow.java:16)
是因为d[i]没有new
for(int i=0;i<10;i++){
d[i] = new Point(Math.random(), Math.random());
//d[i].x=Math.random();
//d[i].y=Math.random();
}Arrays.sort(d);//这里是有问题的,LS的说了,奇怪的是LZ能编译通过?
class PointXY implements Comparable<PointXY>
{
public PointXY(double x, double y)
{
this.x = x;
this.y = y;
}
public int compareTo(PointXY other)
{
if(distance<other.distance)
return -1;
if(distance>other.distance)
return 1;
return 0;
}
public double getDistance()
{
distance = Math.sqrt(x*x+y*y);
return distance;
}
double x,y,distance;
}
public class PointShow
{
public static void main(String[] args)
{
PointXY[] d=new PointXY[10];
double[] dis=new double[10];
for(int i=0;i<10;i++)
{
double xx=Math.random();
double yy=Math.random();
d[i]=new PointXY(xx,yy);
dis[i]=d[i].getDistance();
}
Arrays.sort(dis);
for(int i=0;i<10;i++)
System.out.println(dis[i]);
}
}
初学Java一个月,point类还没学,不过那个错误应该是你用 了point的原因,应该改一下类名