forked from FIT-DNU/Object-Oriented-Programming-with-Java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPtb2.java
More file actions
41 lines (39 loc) · 1.52 KB
/
Ptb2.java
File metadata and controls
41 lines (39 loc) · 1.52 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
package PTB2;
import java.util.Scanner;
public class Ptb2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập hệ số a: ");
double a = sc.nextDouble();
System.out.print("Nhập hệ số b: ");
double b = sc.nextDouble();
System.out.print("Nhập hệ số c: ");
double c = sc.nextDouble();
if (a == 0) {
if (b == 0) {
if (c == 0) {
System.out.println("Phương trình có vô số nghiệm.");
} else {
System.out.println("Phương trình vô nghiệm.");
}
} else {
double x = -c / b;
System.out.println("Phương trình có một nghiệm: x = " + x);
}
} else {
double delta = b * b - 4 * a * c;
if (delta < 0) {
System.out.println("Phương trình vô nghiệm.");
} else if (delta == 0) {
double x = -b / (2 * a);
System.out.println("Phương trình có nghiệm kép: x = " + x);
} else {
double x1 = (-b + Math.sqrt(delta)) / (2 * a);
double x2 = (-b - Math.sqrt(delta)) / (2 * a);
System.out.println("Phương trình có hai nghiệm:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
}
}