— Scott McNealy, Sun Microsystems Chairman, President and ceo


GET  the user  guess CONVERT



Download 40,57 Mb.
Pdf ko'rish
bet116/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   112   113   114   115   116   117   118   119   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

GET 
the user 
guess
CONVERT 
the user guess to 
an 
int
REPEAT 
with 
each cell in the 
int 
array 
IF 
the user guess 
matches
INCREMENT 
the number of 
hits
// FIND OUT
if 
it was the last cell
IF
number of hits 
is 3, 
RETURN 
“kill” 
as the result
ELSE
it was 
not a kill, so 
RETURN
“hit”
ELSE 
RETURN 
“miss” 
1
 prep code test code
real code
real code

2
3
4
1
SimpleDotCom 
class 


you are here
4
writing 

program
105
Just the new stuff
The things we haven’t seen before 
are on this page. Stop worrying! The 
rest of the details are at the end of 
the chapter. This is just enough to let 
you keep going. 
1
Converting a 
String to an int
2
The 
for
loop
The post-increment 
operator
4
break statement
Integer.parseInt(“3”)
A class that 
ships with Java.
A method in the 
Integer class that 
knows how to “parse” 
a String into the int 
it represents.
Takes a String.
for (int cell : locationCells) { }
Declare a variable that will hold one element 
from the array. Each time through the loop
this variable (in this case an int variable named 
“cell”), will hold a different element from the 
array, until there are no more elements (or the 
code does a “break”... see #4 below).
numOfHits++
The ++ means add 1 to 
whatever’s there (in other 
words, increment by 1).
numOfHits++
is the same (in 
this case) as saying numOfHits = 
numOfHits + 1, except slightly 
more efficient.
break;
Gets you out of a loop. Immediately. Right here.
No iteration, no boolean test, just get out now!
 prep code test code
real code
real code

1
2
3
4
The array to iterate over in the loop. 
Each time through the loop, the next 
element in the array will be assigned to 
the variable “cell”. (More on this at the 
end of this chapter.)
Read this for loop declaration as “repeat 
for each element in the ‘locationCells’ 
array: take the next element in the array 
and assign it to the int variable ‘cell’.”
The colon (:) means “in”, so the 
whole thing means “for each int 
value IN locationCells...”


106
 
chapter 5
real code stage
there are no
Dumb Questions
Q:
What happens in 
Integer.parseInt() if the thing you 
pass isn’t a number? And does it 
recognize spelled-out numbers, 
like “three”?
A:
Integer.parseInt() works only 
on Strings that represent the ascii 
values for digits (0,1,2,3,4,5,6,7,8,9). 
If you try to parse something like 
“two” or “blurp”, the code will blow 
up at runtime. (By blow up, we 
actually mean throw an exception
but we don’t talk about exceptions 
until the Exceptions chapter. So for 
now, blow up is close enough.)
Q:

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   112   113   114   115   116   117   118   119   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish