-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.java
98 lines (92 loc) · 3.19 KB
/
Menu.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int chon;
do {
menu();
System.out.print("Chon chuc nang: ");
chon = sc.nextInt();
switch (chon) {
case 1:
giaiPTB1();
break;
case 2:
giaiPTB2();
break;
case 3:
tinhTienDien();
break;
case 4:
System.out.println("Ket thuc chuong trinh.");
break;
default:
System.out.println("Khong co chuc nang nay.");
break;
}
} while (chon != 4);
}
public static void menu() {
System.out.println("+---------------------------------------------------+");
System.out.println("1. Giai phuong trinh bac nhat");
System.out.println("2. Giai phuong trinh bac hai");
System.out.println("3. Tinh tien dien");
System.out.println("4. Ket thuc");
System.out.println("+---------------------------------------------------+");
}
public static void giaiPTB1() {
Scanner sc = new Scanner(System.in);
System.out.print("Nhap a: ");
double a = sc.nextDouble();
System.out.print("Nhap b: ");
double b = sc.nextDouble();
if (a == 0) {
if (b == 0) {
System.out.println("Phuong trinh vo so nghiem.");
} else {
System.out.println("Phuong trinh vo nghiem.");
}
} else {
double x = -b / a;
System.out.println("Nghiem cua phuong trinh la: x = " + x);
}
}
public static void giaiPTB2() {
Scanner sc = new Scanner(System.in);
System.out.print("Nhap a: ");
double a = sc.nextDouble();
System.out.print("Nhap b: ");
double b = sc.nextDouble();
System.out.print("Nhap c: ");
double c = sc.nextDouble();
if (a == 0) {
System.out.println("Day khong phai phuong trinh bac hai.");
return;
}
double delta = b * b - 4 * a * c;
if (delta < 0) {
System.out.println("Phuong trinh vo nghiem.");
} else if (delta == 0) {
double x = -b / (2 * a);
System.out.println("Phuong trinh co nghiem kep: x = " + x);
} else {
double x1 = (-b + Math.sqrt(delta)) / (2 * a);
double x2 = (-b - Math.sqrt(delta)) / (2 * a);
System.out.println("Phuong trinh co 2 nghiem phan biet:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
public static void tinhTienDien() {
Scanner sc = new Scanner(System.in);
System.out.print("Nhap so dien su dung trong thang: ");
int soDien = sc.nextInt();
int tien;
if (soDien <= 50) {
tien = soDien * 1000;
} else {
tien = 50 * 1000 + (soDien - 50) * 1200;
}
System.out.println("So tien phai tra: " + tien + " VND");
}
}