36 lines
1 KiB
Java
36 lines
1 KiB
Java
package de.dhbwka.java.exercise.control_structures;
|
|
|
|
public class Quadratics {
|
|
public static void main(String[] args) {
|
|
java.util.Scanner scan = new java.util.Scanner(System.in);
|
|
|
|
System.out.print("a: ");
|
|
double a = scan.nextDouble();
|
|
System.out.print("b: ");
|
|
double b = scan.nextDouble();
|
|
System.out.print("c: ");
|
|
double c = scan.nextDouble();
|
|
|
|
scan.close();
|
|
|
|
if (a == 0) {
|
|
if (b == 0) {
|
|
System.out.println("Die Gleichung ist degeneriert.");
|
|
} else {
|
|
double x = -c / b;
|
|
System.out.println("x = " + x);
|
|
}
|
|
} else {
|
|
double d = b * b - 4 * a * c;
|
|
if (d >= 0) {
|
|
double x1 = (-b + Math.sqrt(d)) / (2 * a);
|
|
double x2 = (-b - Math.sqrt(d)) / (2 * a);
|
|
System.out.println("x1 = " + x1 + ", x2 = " + x2);
|
|
} else {
|
|
double real = -b / (2 * a);
|
|
double imag = Math.sqrt(-d) / (2 * a);
|
|
System.out.println("x1 = " + real + " + " + imag + "i, x2 = " + real + " - " + imag + "i");
|
|
}
|
|
}
|
|
}
|
|
}
|