forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadratic.java
More file actions
45 lines (38 loc) · 1.32 KB
/
Copy pathQuadratic.java
File metadata and controls
45 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
int a;
int b;
int c;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number ");
if (!in.hasNextInt()) {
System.out.println("Error: \"" + in.next() + "\" is not a valid integer.");
return;
}
a = in.nextInt();
System.out.print("Enter a number ");
if (!in.hasNextInt()) {
System.out.println("Error: \"" + in.next() + "\" is not a valid integer.");
return;
}
b = in.nextInt();
System.out.print("Enter a number ");
if (!in.hasNextInt()) {
System.out.println("Error: \"" + in.next() + "\" is not a valid integer.");
return;
}
c = in.nextInt();
double discriminant = b * b - 4 * a * c;
if (a == 0) {
System.out.println("The value of 'a' cannot be zero.");
} else if (discriminant < 0) {
System.out.println("No real solutions.");
} else {
double x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double x2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
}