问题是求一条南北走向的直线,到各点的距离和最短程序不对,不知道为什么?!package optimalway;
import java.util.*;
//主函数
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
      
       Scanner in=new Scanner(System.in);
       int n=in.nextInt();
       
       Position[] pos=new Position[n];
       for(int i=0;i<n;i++)
           pos[i]=new Position();
       
       for(int i=0;i<pos.length;i++)
         System.out.println("油井的坐标分别为:("+pos[i].getX()+","+pos[i].getY()+") ");
       
       int i,j;
        for(i=1;i<n;i++)
            for(j=n-1;j>=i;j--)
                if(pos[j].getY()>pos[j-1].getX())
               {
                
                Position temp=pos[j];
        pos[j]=pos[j-1];
        pos[j-1]=temp;
            
              }
    
     if(n%2==1)  
            System.out.println(pos[n/2]);
        System.out.println("最优的管道位置是Y:"+pos[n/2]+"到"+pos[n/2+1]);
      }
 }
     class Position{
     public Position(){
       
       Random rand = new Random();
       x=rand.nextInt();
       y=rand.nextInt();
    
     }
     public int getX(){
        return x; 
     }
     public int getY(){
        return y;
     }
     
      private int x;
      private int y;
    }
package optimalway;
import java.util.*;
//主函数
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
      
       Scanner in=new Scanner(System.in);
       int n=in.nextInt();
       
       Position[] pos=new Position[n];
       for(int i=0;i<n;i++)
           pos[i]=new Position();
       
       for(int i=0;i<pos.length;i++)
         System.out.println("油井的坐标分别为:("+pos[i].getX()+","+pos[i].getY()+") ");
       
       int i,j;
        for(i=1;i<n;i++)
            for(j=n-1;j>=i;j--)
                if(pos[j].getY()>pos[j-1].getX())
               {
                
                Position temp=pos[j];
        pos[j]=pos[j-1];
        pos[j-1]=temp;
            
              }
    
     if(n%2==1)  
            System.out.println(pos[n/2]);
        System.out.println("最优的管道位置是Y:"+pos[n/2]+"到"+pos[n/2+1]);
      }
 }
    
//油井的位置坐标类
 class Position{
     public Position(){
       
       Random rand = new Random();
       x=rand.nextInt();
       y=rand.nextInt();
    
     }
     public int getX(){
        return x; 
     }
     public int getY(){
        return y;
     }
     
      private int x;
      private int y;
    }

解决方案 »

  1.   

    给你改了,不知道是不是你说的意思,不过说实话你还是再学习学习基础吧package optimalway; import java.util.*;public class Position {    private int x;
        private int y;    public Position() {
            Random rand = new Random();
            x = rand.nextInt();
            y = rand.nextInt();
        }    public int getX() {
            return x;
        }    public int getY() {
            return y;
        }    /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {        Scanner in = new Scanner(System.in);
            int n = in.nextInt();        Position[] pos = new Position[n];
            for (int i = 0; i < n; i++)
                pos[i] = new Position();        for (int i = 0; i < pos.length; i++)
                System.out.println("油井的坐标分别为:(" + pos[i].getX() + "," + pos[i].getY() + ") ");        int i, j;
            for (i = 1; i < n; i++)
                for (j = n - 1; j >= i; j--)
                    if (pos[j].getY() > pos[j - 1].getX()) {                    Position temp = pos[j];
                        pos[j] = pos[j - 1];
                        pos[j - 1] = temp;                }        if (n % 2 == 1)
                System.out.println(pos[n / 2]);
            System.out.println("最优的管道位置是Y:" + pos[n / 2] + "到" + pos[n / 2 + 1]);
        }
    }