• Lenguaje

    Java usando Scanner

  • Descripción

    Calcula la distancia entre dos puntos de coordenadas conocidas.
    La fórmula final a despejar es:
    D² = (X₂-X₁)² + (Y₂-Y₁)²
    Donde:
    (D) La distancia entre dos puntos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;

public class DistanciaEntreDosPuntosDeCoordenadas {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double distancia, x1, x2, y1, y2;
        System.out.print("Ingresa el valor de x1: ");
        x1 = in.nextDouble();
        in.nextLine();
        System.out.print("Ingresa el valor de x2: ");
        x2 = in.nextDouble();
        in.nextLine();
        System.out.print("Ingresa el valor de y1: ");
        y1 = in.nextDouble();
        in.nextLine();
        System.out.print("Ingresa el valor de y2: ");
        y2 = in.nextDouble();
        in.nextLine();
        distancia=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        System.out.println("Valor de distancia: " + distancia);
    }

}