Sherman123
Fleet Admiral
- Registriert
- Nov. 2002
- Beiträge
- 12.344
Ich will ein Programm schreiben, das mir den Flächeninhalt für ein Rechteck ausgibt. Die Koordinaten der Punkte sind gegeben. Der Einfachheit halber sind die Seiten parallel zur x und y-achse.
Ich habe immer die Koordinaten A(0/0), B(5/0), C(5/3) und D(0/3) genommen. Nun soll das Programm für jedes beliebige Rechteck gelten, das heißt A muss nicht zwingend unten links sein. Somit gibt es 4 verschieden Möglichkeiten.
Leider funktioniert mein Programm nur in Fall 1 und 4. (A = links oben und A = links unten, BCD folgen dann gegen den Uhrzeigersinn)
Ich habe immer die Koordinaten A(0/0), B(5/0), C(5/3) und D(0/3) genommen. Nun soll das Programm für jedes beliebige Rechteck gelten, das heißt A muss nicht zwingend unten links sein. Somit gibt es 4 verschieden Möglichkeiten.
Leider funktioniert mein Programm nur in Fall 1 und 4. (A = links oben und A = links unten, BCD folgen dann gegen den Uhrzeigersinn)
Code:
import javax.swing.JOptionPane;
public class Flaechenrechner3 {
public static void main(String args[])
{
String koordinate1x, koordinate1y, koordinate2x, koordinate2y, koordinate3x, koordinate3y, koordinate4x, koordinate4y;
int ax, ay, bx, by, cx, cy, dx, dy;
double a, b, flaeche;
koordinate1x=JOptionPane.showInputDialog ("X-Koordinate von Punkt A");
koordinate1y=JOptionPane.showInputDialog ("Y-Koordinate von Punkt A");
koordinate2x=JOptionPane.showInputDialog ("X-Koordinate von Punkt B");
koordinate2y=JOptionPane.showInputDialog ("Y-Koordinate von Punkt B");
koordinate3x=JOptionPane.showInputDialog ("X-Koordinate von Punkt C");
koordinate3y=JOptionPane.showInputDialog ("Y-Koordinate von Punkt C");
koordinate4x=JOptionPane.showInputDialog ("Y-Koordinate von Punkt D");
koordinate4y=JOptionPane.showInputDialog ("Y-Koordinate von Punkt D");
ax=Integer.parseInt(koordinate1x);
ay=Integer.parseInt(koordinate1y);
bx=Integer.parseInt(koordinate2x);
by=Integer.parseInt(koordinate2y);
cx=Integer.parseInt(koordinate3x);
cy=Integer.parseInt(koordinate3y);
dx=Integer.parseInt(koordinate4x);
dy=Integer.parseInt(koordinate4y);
if (ax < bx && ax == dx){
a =bx-ax;
b =cy-by;
flaeche=a*b;
JOptionPane.showMessageDialog(null, "Das Rechteck mit den Punkten A: " +ax+"/"+ay+", B: " +bx+"/"+by+", C: " +cx+"/"+cy+" und D: " +dx/dy+ " hat eine Flaeche von " +flaeche+ " m²");
}
if (ax == bx && ax > dx){
a =by-ay;
b =bx-cx;
flaeche=a*b;
JOptionPane.showMessageDialog(null, "Das Rechteck mit den Punkten A: " +ax+"/"+ay+", B: " +bx+"/"+by+", C: " +cx+"/"+cy+" und D: " +dx/dy+ " hat eine Flaeche von " +flaeche+ " m²");
}
if (ax > bx && ay == dy){
a =ax-bx;
b =by-cy;
flaeche=a*b;
JOptionPane.showMessageDialog(null, "Das Rechteck mit den Punkten A: " +ax+"/"+ay+", B: " +bx+"/"+by+", C: " +cx+"/"+cy+" und D: " +dx/dy+ " hat eine Flaeche von " +flaeche+ " m²");
}
if (ax == bx && ax < dx){
a =ay-by;
b =cx-by;
flaeche=a*b;
JOptionPane.showMessageDialog(null, "Das Rechteck mit den Punkten A: " +ax+"/"+ay+", B: " +bx+"/"+by+", C: " +cx+"/"+cy+" und D: " +dx/dy+ " hat eine Flaeche von " +flaeche+ " m²");
}
System.exit(0);
}
}