Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1.  
  2.  
  3. //Hasta aquí es asumir el arreglo y en este programa se le permite ingresar al usuario un objeto TinyCoor
  4.  
  5. System.out.println("Ingrese coordenadas X,Y ej '1,2' ");
  6. String lectura = sc.next();
  7. StringTokenizer stoken= new StringTokenizer (lectura,",");
  8. TinyCoor p = null; //es necesario que este en null para que el usuario no arruine el código
  9. try{
  10. p = new TinyCoor (Short.parseShort(stoken.nextToken()),Short.parseShort(stoken.nextToken()));
  11. }catch(NumberFormatException ex){
  12. System.out.println("Lectura mal ingresada");
  13. System.exit(0); //cierra el programa
  14. }
  15. //Hasta aquí se obtienen los valores asumidos y se procede a calcular
  16. TinyCoor cercano=new TinyCoor ((short)1023,(short)1023);
  17. //Se crea un objeto TinyCoor con la maxima distancia posible
  18. for (int i = 0; i < arreglo.length; i++) {
  19. if (p.distance(arreglo[i])<p.distance(cercano)) cercano=arreglo[i];
  20. //Si la distancia del punto actual es menor al mas cercano entonces se actualiza
  21. }
  22. System.out.println("El punto más cercano es ("+cercano.getX()+","+cercano.getY()+")");
  23. System.out.println("Distancia al punto: "+p.distance(cercano));
  24.  
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement