Add Quadratics class
This commit is contained in:
parent
df9f5fc94c
commit
1a55157dc5
1 changed files with 36 additions and 0 deletions
|
@ -0,0 +1,36 @@
|
|||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue