-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathVoter_Eligibility_Check.java
78 lines (64 loc) · 2.76 KB
/
Voter_Eligibility_Check.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
import java.util.Scanner;
public class VoterAgeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age;
while (true) {
try {
System.out.print("Enter your age: ");
age = Integer.parseInt(scanner.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Invalid age entered. Please enter a valid integer age.");
}
}
if (18 <= age && age <= 120) {
System.out.println("Congratulations!");
System.out.println("You are eligible to vote.");
} else if (12 <= age && age < 18) {
System.out.println("You are not yet eligible to vote.");
System.out.println("Enjoy your teenage years!");
} else if (0 <= age && age < 12) {
System.out.println("You are too young to vote.");
System.out.println("Make the most of your childhood!");
} else if (age < 0) {
System.out.println("Invalid age entered.");
System.out.println("Please enter a positive value.");
} else {
System.out.println("You have surpassed the maximum voting age.");
System.out.println("Thank you for your contribution to society!");
}
scanner.close();
}
}
// -------------------------------END---------------------------------
// The program above is very dynamic while the program below is static
// ------------------------------START--------------------------------
import java.util.Scanner;
public class VoterAgeCheck {
public static void main(String[] args) {
int age;
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter their age
System.out.print("Enter your age: ");
age = scanner.nextInt();
// Check if the age is within a valid range for voting
if (age >= 18 && age <= 120) {
System.out.println("Congratulations!");
System.out.println("You are eligible to vote.");
} else if (age >= 12 && age < 18) {
System.out.println("You are not yet eligible to vote.");
System.out.println("Enjoy your teenage years!");
} else if (age >= 0 && age < 12) {
System.out.println("You are too young to vote.");
System.out.println("Make the most of your childhood!");
} else if (age < 0) {
System.out.println("Invalid age entered.");
System.out.println("Please enter a positive value.");
} else {
System.out.println("You have surpassed the maximum voting age.");
System.out.println("Thank you for your contribution to society!");
}
scanner.close();
}
}