— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet123/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   119   120   121   122   123   124   125   126   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

SimpleDotCom
Game 
class 


you are here
4
writing 

program
111
get user input String
random() and getUserInput()
Two things that need a bit more 
explaining, are on this page. This is 
just a quick look to keep you going; 
more details on the GameHelper 
class are at the end of this chapter.
1
Getting user input 
using the GameHelper 
class
2
Make a random 
number
int randomNum = (int) (Math.random() * 5)
 prep code test code
real code
real code

2
1
This is a ‘cast’, and it forces the thing 
immediately after it to become the 
type of the cast (i.e. the type in the 
parens). Math.random returns a double, 
so we have to cast it to be an int (we 
want a nice whole number between 0 
and 4). In this case, the cast lops off 
the fractional part of the double.
We declare an int variable to hold 
the random number we get back.
A class that comes 
with Java.
A method of the 
Math class.
The Math.random method
returns a number from zero to 
just less than one. So this formula 
(with the cast), returns a number 
from 0 to 4. (i.e. 0 - 4.999.., 
cast to an int)
String guess = helper.getUserInput(“enter a number”);
We declare a String variable to 
hold the user input String we 
get back (“3”, “5”, etc.).
An instance we made earlier
of a class that we built to 
help with the game. It’s called 
GameHelper and you haven’t 
seen it yet (you will).
A method of the GameHelper class 
that asks the user for command-
line input, reads it in after the 
user hits RETURN, and gives back 
the result as a String.
This 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 just 
before the method starts 
looking for user input.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   119   120   121   122   123   124   125   126   ...   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