-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
61 lines (53 loc) · 2.23 KB
/
Copy pathMain.java
File metadata and controls
61 lines (53 loc) · 2.23 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
import java.time.format.DateTimeFormatter;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите фамилию:");
String lastname = scanner.nextLine();
System.out.println("Введите имя:");
String firstname = scanner.nextLine();
System.out.println("Введите отчество:");
String patronymic = scanner.nextLine();
String gender = getGender(patronymic);
System.out.println("Введите дату рождения в формате дд.мм.гггг:");
String birthdate = scanner.nextLine();
scanner.close();
System.out.println("Фамилия и инициалы: " + lastname + " " + firstname.charAt(0) + "." + patronymic.charAt(0) + ".");
System.out.println("Пол: " + gender);
int age = getAge(birthdate);
if (age % 10 == 1 && age % 100 != 11) {
System.out.println("Возраст: " + age + " год");
}
else if (age % 10 > 1 && age % 10 < 5 && (age % 100 < 10 || age % 100 >= 20)) {
System.out.println("Возраст: " + age + " года");
}
else {
System.out.println("Возраст: " + age + " лет");
}
}
private static String getGender(String patronymic) {
if (patronymic.endsWith("ич")) {
return "Мужской";
}
else {
return "Женский";
}
}
private static int getAge(String birthdate) {
LocalDate today = LocalDate.now();
try {
DateTimeFormatter newFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate birthday = LocalDate.parse(birthdate, newFormat);
return Period.between(birthday, today).getYears();
}
catch (DateTimeException ex) {
System.out.println("Введенная дата рождения некорректна!");
System.exit(1);
return -1;
}
}
}