File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class ChainNest {
2+
3+ public static void main (String [] args ) {
4+ int x = 0 ;
5+
6+ if (x > 0 ) {
7+ System .out .println ("x is positive" );
8+ } else if (x < 0 ) {
9+ System .out .println ("x is negative" );
10+ } else {
11+ System .out .println ("x is zero" );
12+ }
13+
14+ if (x > 0 ) {
15+ System .out .println ("x is positive" );
16+ } else {
17+ if (x < 0 ) {
18+ System .out .println ("x is negative" );
19+ } else {
20+ System .out .println ("x is zero" );
21+ }
22+ }
23+ }
24+
25+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ public class DigitUtil {
2+
3+ public static boolean isSingleDigit (int x ) {
4+ if (x > -10 && x < 10 ) {
5+ return true ;
6+ } else {
7+ return false ;
8+ }
9+ }
10+
11+ public static boolean isSingleDigit2 (int x ) {
12+ return x > -10 && x < 10 ;
13+ }
14+
15+ public static void main (String [] args ) {
16+ System .out .println (isSingleDigit (2 ));
17+ boolean bigFlag = !isSingleDigit (17 );
18+
19+ int z = 9 ;
20+ if (isSingleDigit (z )) {
21+ System .out .println ("z is small" );
22+ } else {
23+ System .out .println ("z is big" );
24+ }
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ public class IfElse {
2+
3+ public static void main (String [] args ) {
4+ int x = 17 ;
5+ int n = 18 ;
6+
7+ if (x > 0 ) {
8+ System .out .println ("x is positive" );
9+ }
10+
11+ if (x % 2 == 0 ) {
12+ System .out .println ("x is even" );
13+ } else {
14+ System .out .println ("x is odd" );
15+ }
16+
17+ // look, no braces...bad idea!
18+ if (x % 2 == 0 )
19+ System .out .println ("x is even" );
20+ else
21+ System .out .println ("x is odd" );
22+
23+ if (x > 0 )
24+ System .out .println ("x is positive" );
25+ System .out .println ("x is not zero" );
26+
27+ if (x % 2 == 0 ); { // incorrect semicolon
28+ System .out .println ("x is even" );
29+ }
30+ }
31+
32+ }
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public static void main(String[] args) {
2020
2121 // check the range
2222 double x = in .nextDouble ();
23- if (x >= 0 ) {
23+ if (x > 0 ) {
2424 double y = Math .log (x );
2525 System .out .println ("The log is " + y );
2626 } else {
Original file line number Diff line number Diff line change 1+ public class LogicalOpers {
2+
3+ public static void main (String [] args ) {
4+ int x = 0 ;
5+ int y = 0 ;
6+
7+ // nested
8+ if (x == 0 ) {
9+ if (y == 0 ) {
10+ System .out .println ("Both x and y are zero" );
11+ }
12+ }
13+
14+ // combined
15+ if (x == 0 && y == 0 ) {
16+ System .out .println ("Both x and y are zero" );
17+ }
18+
19+ // chained
20+ if (x == 0 ) {
21+ System .out .println ("Either x or y is zero" );
22+ } else if (y == 0 ) {
23+ System .out .println ("Either x or y is zero" );
24+ }
25+
26+ // combined
27+ if (x == 0 || y == 0 ) {
28+ System .out .println ("Either x or y is zero" );
29+ }
30+
31+ // De Morgan's
32+ if (!(x == 0 || y == 0 )) {
33+ System .out .println ("Neither x nor y is zero" );
34+ }
35+ if (x != 0 && y != 0 ) {
36+ System .out .println ("Neither x nor y is zero" );
37+ }
38+
39+ // boolean variables
40+ boolean flag ;
41+ flag = true ;
42+ boolean testResult = false ;
43+
44+ boolean evenFlag = (x % 2 == 0 ); // true if x is even
45+ boolean positiveFlag = (x > 0 ); // true if x is positive
46+
47+ if (evenFlag ) {
48+ System .out .println ("n was even when I checked it" );
49+ }
50+
51+ if (!evenFlag ) {
52+ System .out .println ("n was odd when I checked it" );
53+ }
54+ }
55+
56+ }
Original file line number Diff line number Diff line change 1+ public class Switch {
2+
3+ public static void main (String [] args ) {
4+ int number = 0 ;
5+ String word ;
6+
7+ // if-else-if
8+
9+ if (number == 1 ) {
10+ word = "one" ;
11+ } else if (number == 2 ) {
12+ word = "two" ;
13+ } else if (number == 3 ) {
14+ word = "three" ;
15+ } else {
16+ word = "unknown" ;
17+ }
18+
19+ // same result as above
20+
21+ switch (number ) {
22+ case 1 :
23+ word = "one" ;
24+ break ;
25+ case 2 :
26+ word = "two" ;
27+ break ;
28+ case 3 :
29+ word = "three" ;
30+ break ;
31+ default :
32+ word = "unknown" ;
33+ break ;
34+ }
35+
36+ System .out .print (number );
37+ System .out .print (word );
38+
39+ // switch blocks fall through
40+
41+ String food = "apple" ;
42+ switch (food ) {
43+ case "apple" :
44+ case "banana" :
45+ case "cherry" :
46+ System .out .println ("Fruit!" );
47+ break ;
48+ case "asparagus" :
49+ case "broccoli" :
50+ case "carrot" :
51+ System .out .println ("Vegetable!" );
52+ break ;
53+ }
54+ }
55+
56+ }
You can’t perform that action at this time.
0 commit comments