-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectricBill.java
More file actions
41 lines (34 loc) · 1.06 KB
/
ElectricBill.java
File metadata and controls
41 lines (34 loc) · 1.06 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 java_core_basic;
import java.util.Scanner;
public class ElectricBill {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int electricNum = 0;
boolean isValidInput = false;
System.out.print("Nhập số điện đã dùng trong tháng: ");
while (!isValidInput) {
String input = sc.nextLine();
try {
electricNum = Integer.parseInt(input);
isValidInput = true;
} catch (NumberFormatException e) {
System.out.print("Số điện phải là số nguyên vui lòng nhập lại: ");
}
}
System.out.println("Tiền điện tháng này là: " + electricBill(electricNum));
sc.close();
}
public static int electricBill(int numElec) {
int price = 0;
for (int i = 1; i <= numElec; i++) {
if (i < 100) {
price += 1000;
} else if (100 <= i && i < 150) {
price += 1500;
} else if (i > 150) {
price += 2000;
}
}
return price;
}
}