Head First Java a brain-Friendly Guide Kathy Sierra and Bert Bates


Q: In the beginning of the book, there was an example of a for loop that



Download 17,18 Mb.
Pdf ko'rish
bet79/90
Sana22.06.2022
Hajmi17,18 Mb.
#693704
1   ...   75   76   77   78   79   80   81   82   ...   90
Bog'liq
(Head Up) Kathy Sierra Bert Bates - Head First Java, 3rd Edition Early Release-O\'Reilly Media, Inc. (2021)

Q: In the beginning of the book, there was an example of a for loop that
was really different from this one—are there two different styles of for
loops?
A:
Yes! From the first version of Java there has been a single kind of 
for
loop (explained later in this chapter) that looks like this:
for (int i = 0; i < 10; i++) { 
// do something 10 times 
}
You can use this format for any kind of loop you need. But... beginning
with Java 5.0 (Tiger), you can also use the 
enhanced
for loop (that’s the
official description) when your loop needs to iterate over the elements in an
array (or 
another
kind of collection, as you’ll see in the 
next
chapter). You
can always use the plain old for loop to iterate over an array, but the
enhanced
for loop makes it easier.
SimpleStartup_and_SimpleStartupTester_public_class_SimpleStartupTestDrive'>Final code for SimpleStartup and
SimpleStartupTester
public class 
SimpleStartupTestDrive



public static void 
main
(String[] args) { 
SimpleStartup dot = new SimpleStartup(); 
int[] locations = {2,3,4}; 
dot.setLocationCells(locations); 
String userGuess = “2”; 
String result = dot.checkYourself(userGuess); 

}
public class 
SimpleStartup

int[] locationCells; 
int numOfHits = 0; 
public void 
setLocationCells
(int[] locs) { 
locationCells = locs; 

public String 
checkYourself
(String stringGuess) { 
int guess = Integer.parseInt(stringGuess); 
String result = “miss”; 
for (int cell : locationCells) { 
if (guess == cell) { 
result = “hit”; 
numOfHits++; 
break; 


// out of the loop
if (numOfHits == 
locationCells.length) { 
result = “kill”; 

System.out.println(result); 
return result; 

// close method

// close class


There’s a little bug lurking here. It compiles and runs, but sometimes...
don’t worry about it for now, but we 
will
have to face it a little later.



Download 17,18 Mb.

Do'stlaringiz bilan baham:
1   ...   75   76   77   78   79   80   81   82   ...   90




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