Download Chapter 5 * Head First Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 5 – Head First Java
The new stuff!
Converting a string to an int
Integer.parseInt(“3”)
A class that ships
w/java
Takes a String
A method in the
Integer class that
knows how to “parse”
a String into the int it
represents
The “For” loop
for (int cell:locationCells) { }
The post-increment operator
numOfHits++
Break statement
break;
Make a Random Number
int randomNum = (int) (Math.random() * 5
Declare an
int
variable to
hold the
random
number
we get
back
This is a ‘cast’,
which forces
the thing
immediately
after it to
become the
type of the
cast. Math
random
returns a
double, so we
have to cast it
to be an int.
A class
that
comes
with
Java
The Math random
methods returns a
number from zero
to just less than
one. So this formula
(with the cast)
returns a number
from 0 to 4.
Getting User Input
String guess = helper.getUserInput(“Enter number);
Declare a
String
variable
to hold
the user
input of
String
A method
that asks
the user for
commandline input,.
A method of a
class called
“GameHelper”
to ask for user
input
Method takes a
String argument
that it uses to
prompt the user
at the
command line.
Whatever you
pass in here
gets displayed
in the terminal.
Regular (non-enhanced) for loops
for (int i = 0; i<100; i++) { }
Initialization
Boolean
Test
Iteration
Expression
The code
to repeat
goes here
(the body
Enhanced For Loop
for (String name : nameArray) { }
The enhanced for loop makes it easier to iterate over all
the elements in an array;
“For each element in nameArray,
assign the element to the ‘name’
variable, and run the body of the loop”