-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathifelse.java
More file actions
25 lines (21 loc) · 788 Bytes
/
ifelse.java
File metadata and controls
25 lines (21 loc) · 788 Bytes
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
import java.util.Scanner;
public class ifelse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
// Input validation
while (!sc.hasNextInt()) {
System.out.println("Invalid input! Please enter a valid age (a positive integer).");
sc.next(); // Clear the invalid input
System.out.print("Enter your age: ");
}
int age = sc.nextInt();
if (age >= 18) {
System.out.println("Adult");
} else if (age > 0) {
System.out.println("Child");
} else {
System.out.println("Invalid age! Age must be a positive number.");
}
}
}