| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 363 人关注过本帖
标题:随即函数求距离的疑惑,有很多不懂的,求解释
只看楼主 加入收藏
林河
Rank: 1
等 级:新手上路
帖 子:14
专家分:5
注 册:2011-10-26
结帖率:50%
收藏
 问题点数:0 回复次数:1 
随即函数求距离的疑惑,有很多不懂的,求解释
public class Point {
            int x,y;
            public Point(int x,int y){
                this.x=x;
                this.y=y;
                }
            public int distance(){
                return (int)Math.sqrt(x*x+y*y);
            }
            public static void main(String args[])
            {
                  Point p[]=new Point[9];
                  for(int i=0;i<p.length;i++)
                  {
                      p[i]=new Point((int)(Math.random()*10+1),(int)(Math.random()*10+1));//产生随机的点
                  }
                  Point temp = null;//排序        
                  for (int i = 0; i < 8; i++)
                  {            
                      for (int j = i + 1; j < 9; j++)
                      {               
                          if (p[i].distance() > p[j].distance())
                          {                    
                              temp = p[i];                    
                              p[i] = p[j];                    
                              p[j] = temp;               
                          }        
                      }
                  }
                  for (int i =0; i <9; i++)
                  {            
                      System.out.print(i+1+".产生的点("+p[i].x+","+p[i].y+") 到原点的距离是:");
                      System.out.println(p[i].distance());        
                  }
            }
            
}
老师处了个作业,在网上找到这个代码,但是有很多看不懂的,比如说this,还有p[i].distance() > p[j].distance()这一个是怎么调用前面的distance()方法的,distance()里面的x和y的值又是怎么和数组联系起来运算的。真心不懂了,大侠给点指示
搜索更多相关主题的帖子: void distance public return 
2012-04-04 21:44
芥末小弟
Rank: 2
来 自:北京
等 级:论坛游民
帖 子:58
专家分:92
注 册:2013-1-19
收藏
得分:0 


public class Point {
            int x,y;
            public Point(int x,int y){//这是构造方法
                this.x=x;
                this.y=y;
                }
            public int distance(){//这是求解随即生成的点到原点的距离
                return (int)Math.sqrt(x*x+y*y);
            }
            public static void main(String args[])
            {
                  Point p[]=new Point[9];
                  for(int i=0;i<p.length;i++)
                  {
                      p[i]=new Point((int)(Math.random()*10+1),(int)(Math.random()*10+1));//产生随机的点
                  }
                  Point temp = null;//排序        
                  for (int i = 0; i < 8; i++)
                  {            
                      for (int j = i + 1; j < 9; j++)
                      {               
                          if (p[i].distance() > p[j].distance())
                          {                    
                              temp = p[i];                    
                              p[i] = p[j];                    
                              p[j] = temp;               
                          }        
                      }
                  }
                  for (int i =0; i <9; i++)
                  {            
                      System.out.print(i+1+".产生的点("+p[i].x+","+p[i].y+") 到原点的距离是:");//循环输出
                      System.out.println(p[i].distance()); // 此处循环一次,
                //那么p[i]就实例化了就是一个Point类的一个对象,那么就可以调用此类里的distance方法      
                  }
            }
            
}
不懂,再问    qq   1258601435

开发者。。
QQ:690591637
2013-01-19 20:34
快速回复:随即函数求距离的疑惑,有很多不懂的,求解释
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046577 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved