-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh04_19_SwitchCase.java
More file actions
85 lines (76 loc) · 3.34 KB
/
Ch04_19_SwitchCase.java
File metadata and controls
85 lines (76 loc) · 3.34 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
import java.util.Scanner;
public class Ch04_19_SwitchCase {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//If-Else Ladder //Note to avoid entering max value = put highest value first , Example written below;
System.out.print("Enter your engine displacement: ");
int EngDispl = input.nextInt();
if (EngDispl > 0 && EngDispl < 1000) {
System.out.println("Tax is 15%");
}
else if (EngDispl >= 1000 && EngDispl < 1200) {
System.out.println("Tax = 18%");
}
else if (EngDispl >= 1200 && EngDispl < 1500) {
System.out.println("Tax = 22%");
}
else if (EngDispl >= 1500 && EngDispl < 2000) {
System.out.println("Tax = 28%");
}
else {
System.out.println("Tax = 30%");
}
System.out.println("************");
//Example of decreasing value
// Opposite doesn't work because java goes step by step downwards i.e. lower value above will stick to the result
System.out.print("Enter your engine displacement: ");
int EngDispl1 = input.nextInt();
if (EngDispl1 > 2000) {
System.out.println("Tax is 30%");
} else if (EngDispl > 1500) {
System.out.println("Tax = 28%");
} else if (EngDispl > 1200) {
System.out.println("Tax = 22%");
} else if (EngDispl > 1000) {
System.out.println("Tax = 18%");
} else {
System.out.println("Tax = 15%");
}
System.out.println("***********");
//Switch Case - used when we have to make choice from alternative codes in a program
System.out.print("Enter your engine displacement: ");
int EngDispl2 = input.nextInt();
// Switch variable can be an in , char or a String and can be a whole code inside a Case
switch (EngDispl2) { //Break helps to get out of the switch case , if not used all case will be executed
case 2000:
System.out.println("You have to pay a tax of 30%");
System.out.println("But you will get a lease of 10%");
break;
case 1500:
System.out.println("You have to pay a tax of 28%");
break;
case 1200:
System.out.println("You have to pay a tax of 22%");
break;
case 1000:
System.out.println("You have to pay a tax of 18%");
break;
default:
System.out.println("You have to pay a tax of 15%");
break;
}
//Switch also has an enhanced version which is
System.out.println("********");
System.out.println("Enhanced Switch statement's code:");
switch (EngDispl2) {
case 2000 -> { // Doesn't require a break statement
System.out.println("You have to pay a tax of 30%");
System.out.println("But you will get a lease of 10%");
}
case 1500 -> System.out.println("You have to pay a tax of 28%");
case 1200 -> System.out.println("You have to pay a tax of 22%");
case 1000 -> System.out.println("You have to pay a tax of 18%");
default -> System.out.println("You have to pay a tax of 15%");
}
}
}