Read the four values corresponding to the axes x
and y
of any two points in the plane p1(x1,y1)
and p2(x2,y2)
and calculate the distance between them, showing 4 decimal places after the comma, according to the formula: / p>
Theinputfilecontainstworowsofdata.Thefirstlinecontainstwofloatingpointvalues:x1y1
andthesecondlinecontainstwofloatingpointvaluesx2y2
.OutputCalculateandprintthedistancevalueaccordingtotheformulaprovided,with4housesafterthedecimalpoint.
InputSample
1.07.05.09.0saida=4.4721entrada-2.50.412.17.3saída=16.1484
Mycodeisthisway
importjava.util.*;publicclassProblema{publicstaticvoidmain(String[]args){Scannerentrada=newScanner(System.in);Formatterformato=newFormatter(Locale.ENGLISH);StringvaloresX;StringvaloresY;valoresX=entrada.nextLine();//pegavaloresda1ºlinhanocaso-2.50.4valoresY=entrada.nextLine();//pegavaloresda2ºlinhanocaso12.17.3String[]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 I pass -2.5 (space) 0.4 on the 1st line and 12.1 (space) 7.3 on the 2nd line and the result returned and NaN, I need the output to be 16.1484.