1
Read the four values corresponding to the axes x
and y
of any two points in the plan p1(x1,y1)
and p2(x2,y2)
and calculate the distance between them, showing 4 decimal places after the comma, according to the formula:
The input file contains two lines of data. The first line contains two floating point values: x1 y1
and the second line contains two floating point values x2 y2
.
Exit
Calculate and print the distance value according to the given formula, with 4 boxes after decimal point.
Input Example
1.0 7.0 5.0 9.0 saida = 4.4721 entrada -2.5 0.4 12.1 7.3 saída = 16.1484
My code is that way
import java.util.*;
public class Problema {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
Formatter formato = new Formatter(Locale.ENGLISH);
String valoresX;
String valoresY;
valoresX = entrada.nextLine(); //pega valores da 1º linha no caso -2.5 0.4
valoresY = entrada.nextLine();//pega valores da 2º linha no caso 12.1 7.3
String[] eixosX = valoresX.split(" "); // aqui jogo os valores separados de x em cada posição do vetor eixosX
double x1 = Double.parseDouble(eixosX[0]); //Converto a string para double
double x2 = Double.parseDouble(eixosX[1]);//o mesmo aqui
String[] eixosY = valoresY.split(" "); // mesmos passos acima mas agora para y
double y1 = Double.parseDouble(eixosY[0]);
double y2 = Double.parseDouble(eixosY[1]);
double distancia = Math.sqrt((x2 - x1) * 2 + (y2 - y1) * 2);
formato.format("%.4f", distancia);
System.out.println(formato);
entrada.close();
}
}
my difficulty is in the example of the second entry where step -2.5 (space) 0.4 in the first line and 12.1(space) 7.3 in the second line and the result returned and Nan, I need the output to be 16.1484.