Java :: Aufgabe #35 :: Lösung #6
6 Lösungen
#35
Abstand zweier Punkte
Anfänger - Java
von Dome
- 03.01.2013 um 01:09 Uhr
Schreiben Sie ein Programm, welches den Abstand zweier Punkte berechnet. Zuvor müssen die Koordinaten beider Punkte abgefragt werden.
Konsolenausgabe:
x1:1
y1:1
x2:2
y2:2
1.4142135623730951
#6
von paddlboot (3970 Punkte)
- 09.07.2019 um 09:19 Uhr
import java.util.*;
public class AbstandPunkte {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x1, x2, y1, y2;
System.out.print("X1:\t");
x1 = scanner.nextInt();
System.out.print("Y1:\t");
y1 = scanner.nextInt();
System.out.print("X2:\t");
x2 = scanner.nextInt();
System.out.print("Y2:\t");
y2 = scanner.nextInt();
System.out.print(abstand(x1, x2, y1, y2));
scanner.close();
}
public static double abstand(int x1, int x2, int y1, int y2) {
double sum = Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2);
return Math.sqrt(sum);
}
}Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
