Java for Fucking Idiots: Learn the Basics of Java Programming Without any experience!



Download 3,91 Mb.
Pdf ko'rish
bet24/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   20   21   22   23   24   25   26   27   ...   70
Bog'liq
Java for Fucking Idiots

if
(keyPressed ==
'W'
) {
player.moveForward();
}
else if
(keyPressed ==
'A'
) {
player.moveLeft();
}
else if
(keyPressed ==
'S'
) {
player.moveBackward();
}
else if
(keyPressed ==
'D'
) {
player.moveRight();
}
else
{
player.stopMoving();
}
And behold the new and totally correct way to do it, with no mistakes
whatsoever:
switch
(keyPressed) {
case
'W'
:
player.moveForward();
case
'A'
:
player.moveLeft();


case
'S'
:
player.moveBackward();
case
'D'
:
player.moveRight();
default
:
player.stopMoving();
}
Whoa, crazy! We create a “switch ” with the value of the user input, then do
something different for each “case ”. And no, “case” has nothing do with
uppercase or lowercase letters; it’s when the switch equals a particular
value.
If they pressed in ‘W’, then the code under the ‘W’ case is run.
If they pressed ‘D’, then the code under the ‘D’ case is run.
The “default ” is what happens if the user input isn’t any of our cases.
Maybe they pressed space bar. Or some letter we haven’t accounted for.
Make sense? Compare it with the if/else examples further back.
However, there’s one fatal flaw in the switch example code (IT’S
ACTUALLY NOT WITHOUT MISTAKES, CURVEBALL!!!). What
happens if the user presses ‘A’? What direction does the player move?
Think about it. Scroll up/flip back/take a taxi back and look; what could be
wrong with a user pressing ‘A’? Or don’t. I’ll tell you anyway.
The player starts to 
moveLeft()
but is then immediately told to 
stopMoving()
.
“That’s what’s happened?! But why?? The user pressed ‘A’, so it matched
the ‘A’ case, right? Move left?” – you, maybe.
Well, the “
default
” line of code will actually happen every single time, in
addition to the “case”…
Because unless you specify otherwise, a switch will look at every single
‘case’, then hit the ‘default’ block of code. That switch won’t stop!


You need to give a very special word to Java here for each ‘case’. It’s called
break .

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   20   21   22   23   24   25   26   27   ...   70




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