You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let netprice = discount(200.00, 'HALFOFF'); // would be 100.
263
+
int netprice = discount(200.00, 'HALFOFF'); // would be 100.
264
264
----
265
265
266
266
But think about adding another discount, we'd have to add another `if`, more braces, and make sure we nest it in there carefully, otherwise we break the whole, rickety, mess.
Copy file name to clipboardExpand all lines: arrays.adoc
+23-23Lines changed: 23 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,11 @@ Now, if you're the coder of this game you could keep track of each player's heal
7
7
8
8
[source]
9
9
----
10
-
let playerZeroHealthScore = 100;
11
-
let playerOneHealthScore = 100;
12
-
let playerTwoHealthScore = 100;
13
-
let playerThreeHealthScore = 100;
14
-
let playerFourHealthScore = 100;
10
+
int playerZeroHealthScore = 100;
11
+
int playerOneHealthScore = 100;
12
+
int playerTwoHealthScore = 100;
13
+
int playerThreeHealthScore = 100;
14
+
int playerFourHealthScore = 100;
15
15
----
16
16
17
17
If we setup these 5 variables, our game can track 5 players! But *we'd have to change the game's code to track SIX players.* Well, that not good. Kind of silly actually.
@@ -20,15 +20,15 @@ To get around this kind of problem we use an *array*. We could ask, "how many pl
20
20
21
21
[source]
22
22
----
23
-
let playerHealthScores = [100, 100, 100, 100, 100];
23
+
int playerHealthScores = [100, 100, 100, 100, 100];
24
24
----
25
25
26
26
Now, like a _string_, array indexes start at zero.
27
27
28
28
[source]
29
29
----
30
30
// 0 1 2 3 4
31
-
let playerHealthScores = [100, 100, 100, 100, 100];
31
+
int playerHealthScores = [100, 100, 100, 100, 100];
32
32
----
33
33
34
34
This array is a *data structure* - a way for us to keep track of lots of data in a controlled fashion. (We can make arrays any size.)
@@ -63,11 +63,11 @@ Declaring and initializing some arrays in Java:
63
63
64
64
[source]
65
65
----
66
-
let donuts = ["chocolate", "glazed", "jelly"];
66
+
int donuts = ["chocolate", "glazed", "jelly"];
67
67
68
-
let arrayofLetters = ['c','h','r','i','s'];
68
+
int arrayofLetters = ['c','h','r','i','s'];
69
69
70
-
let mixedData = ['one', 2, true]; // a string, a number and a boolean!
70
+
int mixedData = ['one', 2, true]; // a string, a number and a boolean!
71
71
----
72
72
73
73
=== Accessing elements of an Array
@@ -77,23 +77,23 @@ strings to identify our donuts. Sometimes, we say something like "donuts sub 2"
77
77
78
78
[source]
79
79
----
80
-
let donuts = ["chocolate", "glazed", "jelly"];
80
+
int donuts = ["chocolate", "glazed", "jelly"];
81
81
82
-
console.log(donuts[0]); // "chocolate" (we could say "donuts sub zero")
82
+
System.out.println(donuts[0]); // "chocolate" (we could say "donuts sub zero")
83
83
84
-
console.log(donuts[2]); // "jelly"
84
+
System.out.println(donuts[2]); // "jelly"
85
85
----
86
86
=== Adding to an Array
87
87
88
88
We can also add things to the end of the array.
89
89
90
90
[source]
91
91
----
92
-
let donuts = ["chocolate", "glazed", "jelly"];
92
+
int donuts = ["chocolate", "glazed", "jelly"];
93
93
94
94
donuts[3] = "strawberry" // notice there is no element 3 before this,
95
95
96
-
console.log(donuts); // but after, there are now 4 things in the array.
96
+
System.out.println(donuts); // but after, there are now 4 things in the array.
97
97
----
98
98
99
99
=== Get the size of an Array
@@ -102,9 +102,9 @@ We can use the *length* property to find the size of an array.
Copy file name to clipboardExpand all lines: consolelog.adoc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,7 @@ public class Main {
68
68
}
69
69
----
70
70
71
-
That comment at the end of the console.log line is showing what you can expect to see in the output. Here it would be "3.5" printed by itself. Try it in your bookmarked Repl.
71
+
That comment at the end of the System.out.println line is showing what you can expect to see in the output. Here it would be "3.5" printed by itself. Try it in your bookmarked Repl.
0 commit comments