Hallo,
ich möchte mich schonmal im vorfeld entschuldigen... das ist eine wirklich dumme Frage aber es dreht sich um eine simple if-Schleife. Problem ist das Lösen einer quadratischen Gleichung.
Das Problem ist nun... dass zunächst der Fall wenn die Diskriminante kleiner als 0 ist nicht beachtet wird. Die beiden anderen Fälle funktionieren und es wird auch das richtige Ergebnis ausgegeben. Ausserdem gehört das "else" (ganz unten) noch zum ersten if (a> 0.0000001)... wird allerdings immer ausgegeben.
Ich vermute ein simples klammerungsproblem... Kann mir jmd helfen?
Anxiety
ich möchte mich schonmal im vorfeld entschuldigen... das ist eine wirklich dumme Frage aber es dreht sich um eine simple if-Schleife. Problem ist das Lösen einer quadratischen Gleichung.
Code:
public class QuadGleichung {
/**
* Methode um eine quadratische Gleichung zu lösen
*/
public static void main() {
double a;
double b;
double c;
double p;
double q;
double D; // Diskriminante
double x1; //1. Ergebniss
double x2; //2. Ergebniss
a = TastaturEingabe.readDouble("ax²+bx+c=0\nBitte a eingeben.\na: ");
b = TastaturEingabe.readDouble("Bitte b eingeben.\nb: ");
c = TastaturEingabe.readDouble("Bitte c eingeben.\nc: ");
p=b/a;
q=c/a;
D = Math.sqrt(Math.pow((p/2),2)-q);
if (a> 0.0000001){
if (D>0) {
System.out.println("\n2 reele Lösungen");
x1=(-p/2)+Math.sqrt(Math.pow((p/2),2)-q);
x2=(-p/2)-Math.sqrt(Math.pow((p/2),2)-q);
x1=Math.round( x1 * 100. ) / 100.; //runden auf 2 Nachkommastellen
x2=Math.round( x2 * 100. ) / 100.;
System.out.println("\nx1= " + x1 + " x2= " + x2);
}
if (D==0) {
System.out.println("\n1 reele Doppellösunge");
x1=(-p/2)+Math.sqrt(Math.pow((p/2),2)-q);
x2=(-p/2)-Math.sqrt(Math.pow((p/2),2)-q);
x1=Math.round( x1 * 100. ) / 100.; //runden auf 2 Nachkommastellen
x2=Math.round( x2 * 100. ) / 100.;
System.out.println("\nx1= " + x1 + " x2= " + x2);
}
if (D<0) {
System.out.println("\n1 komplexe Lösungen");
x1=(-p/2)+Math.sqrt(Math.pow((p/2),2)-q);
x2=(-p/2)-Math.sqrt(Math.pow((p/2),2)-q);
x1=Math.round( x1 * 100. ) / 100.; //runden auf 2 Nachkommastellen
x2=Math.round( x2 * 100. ) / 100.;
System.out.println("\nx1= " + x1 + " x2= " + x2);
}
else {
System.out.println(a);
}
}
}
}
Das Problem ist nun... dass zunächst der Fall wenn die Diskriminante kleiner als 0 ist nicht beachtet wird. Die beiden anderen Fälle funktionieren und es wird auch das richtige Ergebnis ausgegeben. Ausserdem gehört das "else" (ganz unten) noch zum ersten if (a> 0.0000001)... wird allerdings immer ausgegeben.
Ich vermute ein simples klammerungsproblem... Kann mir jmd helfen?
Anxiety