-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ4.java
More file actions
104 lines (96 loc) · 3.32 KB
/
Copy pathJ4.java
File metadata and controls
104 lines (96 loc) · 3.32 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
> Date Created: 18/04/2026
> Author: Ishaan Rastogi
> Purpose: To showcase conditionals in Java
> Operating System: This is only for Windows OS, it may or may not work on other OS
> Program Status: 100% Working
*/
import java.util.*;
public class J4 {
public static void main(String[] args) {
// If
int a = 10;
if (a > 5) {
System.out.println("a is greater than 5");
}
// If-Else
if (a < 0) {
System.out.println("a is negative");
} else {
System.out.println("a is positive");
}
// If-Else-If
Scanner sc = new Scanner(System.in);
System.out.println("Enter your age");
int age = sc.nextInt();
if (age >= 18) {
System.out.println("Adult");
} else if (age < 18 && age > 0) {
System.out.println("Not Adult");
} else {
System.out.println("Invalid Age");
}
// Nested If-Else
System.out.println("Do you have LeetCode Premium?");
boolean hasLCpremium = sc.nextBoolean();
System.out.println("Enter the number of problems you have solved");
int solvedProblems = sc.nextInt();
if (hasLCpremium) {
if (solvedProblems >= 200) {
System.out.println("Unlock Advanced Sheet");
} else {
System.out.println((200 - solvedProblems) + " more problems to go to unlock Advanced Sheet");
}
} else {
System.out.println("Upgrade to Premium");
}
// Ternary Operator - (Condition) ? what to return when true: what to return
// when false
System.out.println("Enter the no. of streak days");
int streakDays = sc.nextInt();
String status = (streakDays >= 21) ? "Consistent" : "Irregular";
System.out.println("Your status is: " + status);
// Switch
System.out.println("Enter the day number of the week");
int n = sc.nextInt();
switch (n) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid Day");
}
// continue - will skip the current and move on
// Advanced Switch
System.out.println("Enter the day number of the week");
int x = sc.nextInt();
switch (x) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
case 3 -> System.out.println("Wednesday");
case 4 -> System.out.println("Thursday");
case 5 -> System.out.println("Friday");
case 6 -> System.out.println("Saturday");
case 7 -> System.out.println("Sunday");
default -> System.out.println("Invalid Day");
}
}
}