Repository navigation
Expand file tree
/
Copy pathLesson7_ifStatements.java
More file actions
59 lines (47 loc) · 1.61 KB
/
Copy pathLesson7_ifStatements.java
File metadata and controls
59 lines (47 loc) · 1.61 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
import java.util.Scanner;
public class Lesson7_ifStatements {
public static void main(String[] args) {
// if statement = performs a block of code is its condition is true
Scanner sc = new Scanner(System.in);
String name;
int age;
boolean isStudent;
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.print("Enter your age: ");
age = Integer.parseInt(sc.nextLine());
System.out.print("Are you a student (true/false): ");
isStudent = Boolean.parseBoolean(sc.nextLine());
// GROUP 1
if(name.isEmpty()) { // checks if string variable is empty and returns true/false
System.out.println("You didn't enter your name! ");
}
else {
System.out.println("Hello " + name + '!');
}
// GROUP 2
if(age >= 18) {
System.out.println("You are an adult!");
}
else if(age >= 65) { // if-statements are checked top-down, meaning the first statement that is true will run and end the if-structure
System.out.println("You are a senior!");
}
else if(age < 0) {
System.out.println("You haven't been born yet!");
}
else if(age == 0) {
System.out.println("You are a baby!");
}
else {
System.out.println("You are a child!");
}
// GROUP 3
if(isStudent) {
System.out.println("You are a student!");
}
else {
System.out.println("You are NOT a student!");
}
sc.close();
}
}