Uploaded by 李晨碩

LJ14 JAVA基礎程式設計

advertisement
Java ⟡䣵䧲⻶娔姯
⷏⌇曢免媙䧲䟻䙣⛿晱
]]]VIYINUURIUSZ]
奰䣉⟥䱯
䧪Ჾ⛀濕-DYDょ䏦䣉ⷍ䫟ᶉ
䧪ᵊ⛀濕壹墡Ʊ宅㑗Ʊ婈㐶
䧪᳇⛀濕 -DYD弉䩕⨎
䧪▙⛀濕-DYD㬸䨔㉥⃴
䧪ᵒ⛀濕-DYD庲◆㉥⃴
䧪Ჾ⛀濕-DYDょ䏦䣉ⷍ䫟ᶉ
⑬
㡀妔
•⑬
䖬㣗
• ⹜䏝㯹ᳫᵄ墡ᵾ浺㓭-DYD
• ᵾ浺㓭-DYD6(Ʊ-DYD0(Ʊ-DYD((
• -DYD6(⚸㗪㙴㢉
• -DYD奜墾䕂䄷⻥
• 摉䔺-DYDヾ⺃曆䕂㯿䣉
ᵾ浺㓭-DYD"
• 㽸ḓ方値岝ㄉ㤝徻㗇-DYD"
• -DYD㓭ᵾ浺㘯堽"㓭方値䕂士壆䣉ⷍ彂㓭
• 妉␍-DYD䣉ⷍ㓭ᵾ浺"弈ヰ⒌"㓭ㄉ㤝⺃ὗ⅝侻⒌"
• 㖶⫾⳶䗉⃮审Ჾ⛄-DYD㖶濕彂㓭᳋㳃㠘⩁䕂Ḛ䏦濊
• -DYD㓭ᵾ浺㣡䕂奜㮓⌠濨$SSOHW⍊6FULSW䕂Ⲭ⃣㓭ᵾ浺濨
• ハ᳋㓭⫆㠫ᵸ➩濕⟨⫆㠫䕂䨒㜆濕ハ⊭侻䗉᳋む濊
㽸ḓ方値岝ㄉ㤝徻㗇-DYD濨
• 㗇Ჾỉ⚵圊䍮❁⊭ᶣ⚵圊-DYDヾ㍮⪩䕂䣉ⷍ濕彡浺⽦⫯⊭
ᶣ◦方値ピㄉ㤝᳈弉圊-DYDょ䏦䣉ⷍ濕⇃ㆪ弈ヰƱ↓䐩Ʋ
⛽䈮Ὥ㹷Ɲhttps://handy-games.com/en/gaming-nostalgia-java-games-by-handygames/
⛽䈮Ὥ㹷Ɲhttps://www.youtube.com/watch?v=CadxM2B6zy8&ab_channel=CodeBode
㓭方値䕂士壆䣉ⷍ彂㓭ġ
• -DYD䣉ⷍ奜墾濑-DYD
SURJUDPPLQJODQJXDJH濒
尵㵎㑺ⴲ濕≝ 坩䣯㽸
ĩ2DNĪ濕㓭䐴㔀*UHHQ
WHDP嶝榒摉䔺◖效ᳫ䏦Ṅ㍮⪩
6WDUㄉㆿ士ὗ᳈ょ䏦䣉ⷍ䕂
Ჾỉ䣉ⷍ奜墾Ʋ
⛽䈮Ὥ㹷ƝOracle
㓭方値䕂士壆䣉ⷍ彂㓭ġ
• ハ䗉⃮䥕ヴ⟔映㗇朄㤟㣷濕
ヾᶣ⫅⩁⊔⋋2DN濕Ḅ⹊Ṅ
䔺䊼2DN廗ỉ⋋䣯Ⲱ䱑坩壹
⁈ḽ䏦ᵄ濕⟥⩴⑛匕⍔␟彈
壌妔㑮䕂⋋䣯濕ㄋ昆㤝Ჾ↓
䕂⫅ᴉ㏷⋋㽸ĩ-DYDĪƲ
⛽䈮Ὥ㹷ƝOracle
James Gosling
弈ヰ⒌"㓭ㄉ㤝⺃ὗ⅝侻⒌"
• ㄉ㤝ᳫ䕂-DYD䣉ⷍ婑⟥⩴⫋
-DYD仱䁝侻失
• ᳤᳋㓭ヾ㗇䕂ㄉ㤝徻㏭㊲
-DYD濕⺃曆㗇⚵圊䍮❁
• -DYDᴝ᳋⊨㓭㽸ᵄ㍮⪩弈ヰ濕
圊ᵉ㖄Ʊ佟䠦∱㔀圊⾃⃄㙎Ʊ
方⨎徳ᶴ嶝榒䨇濕ᴝ徻⊭ᶣ
䏦-DYDṄ㍮⪩⚵圊Ʋ
⛽䈮Ὥ㹷Ɲhttps://www.rishabhsoft.com/blog/understanding-javaapplication-development
㖶⫾⳶䗉⃮审Ჾ⛄-DYD㖶濊
• ⽦䗉⃮㖶⫾审Java䕂㖶屈⟘濕圦䟸䏝㯹ᳫ屈⟘㘯堽ょ䏦
ᵄJavaㄾ圑濕䏝㯹ᳫ㿟唓᳋㓭Java濕廗㓭䗝䕂濊
⛽䈮Ὥ㹷ƝOracle
:KDWLV-DYD⟨⫆㠫䕂䨒㜆濕ハ⊭侻䗉᳋む濊
• 㓭Ჾ䣬ㄾ圑濑廗㘯堽䏦ᵄ-DYD濒
• 㓭Ჾ䣬䣉ⷍ奜墾濑⽦⊭ᶣ䏦-DYD⪩ỉ䣉ⷍ濒
• ⊭㍮⪩ょ䏦䣉ⷍ濑廗䣉ⷍ㓭䏦-DYD摉䔺䕂濒
• 㓭ㄉ㤝᳈䕂Ჾ曃审涜濑廗㧼ㄉ㤝㏭㊲-DYD⑒濒
• 㓭Ჾ䣬妾䏝ⲣ‵濑ハ㗁-DYD濕ハ⾱ㄼⲣḚ濒
ᵾ浺㓭-DYD6(Ʊ-DYD0(Ʊ-DYD((
• ㊎ṙᵄ⩊㐲䕂-DYDㄾ圑䏠⍿濕ṛⳀ⛲斾㬀᳹堿⇾⃄㽸᳇
ỉ᳋⋊䕂䣬朜濕⇃ㆪᵄ
• -DYD6(㣗㵔䄆濑⑬㤝Ḛ㠫Ʋ㗪奰䣉ᶣ㨢ⴱ⊮㽸㵔濒
• -DYD0(巓悍䄆濑0RELOHƱ3'$䨇圊↓垛丬濒
• -DYD((ᶿ㠫䄆濑:HE新䦭ょ䏦濒
-DYD6(㙴㢉
⛽䈮Ὥ㹷ƝOracle
-'.
• …⋋㓭-DYD6('HYHORSPHQW.LWV濕
ᳫ㑅⋋㽸-DYD㣗㵔䄆摉䔺ⲣ‵䩯Ʋ
• ㊎ṙ䲦娭┦濑&RPSLOHU濒帇㊙榖
敌奜墾㽸巁Ḍ敌䕂奜墾ᴉ⟔濕彂㊎
ṙ㗇‴⩁䖶撚䕂摉䔺Ʊ⚵圊Ʊ㳪夤
䨇ⲣ‵Ʋ
• ◦㍮⪩⠻-DYD䣉ⷍ䕂≝⢉䝺
濑6RXUFHFRGH濒ᴉ⹊濕彂⺃曆⩇
垛-'.濕ㄋ⊭ᶣ廰圊㉣᳉Ṅ䕂䲦娭Ʊ
㳪夤䨇ⲣḚƲ
-5(
• …⋋㽸-DYD5XQWLPH
(QYLURQPHQW濕ᳫ㑅⋋㽸-DYD⚵圊
䍮❁Ʋ
• ㊎ṙ-DYDょ䏦䣉ⷍ䖶撚宅㵎䕂Ჾỉ
䍮❁濕ᾍ㓭㣗㵔朜⃣⵩濑&ODVV
OLEUDU\濒濕-DYD唙㎪㤝┦䨇Ʋ
-90
• …⋋㽸-DYD9LUWXDO
0DFKLQH濕⇃ㆪ◦-5(ᴉᳫƲ
• ㍮⪩᳤䲦娭⠻䕂-DYD䣉ⷍ㗁
⫅-90䐴ḚᲾ⊮䗝⪤䕂㤝┦Ʋ
• 婑-DYD䣉ⷍ⊭ᶣ弉圊㑺⋂ỉ
ⴱ⊮ᴉ᳈Ʋ
-DYD奜墾䕂䄷⻥
• 䫟⑬䕂濑6LPSOH濒
• 䄧ᶴ⫌⋏䕂濑2EMHFWRULHQWHG濒
• ⩇…䕂濑6HFXUH濒
• ⟘⚵圊䲐䕂濑0XOWLWKUHDG濒
• 岦ⴱ⊮濑3ODWIRUPLQGHSHQGHQW濒
-DYD奜墾
• -DYD䣉ⷍ奜墾⫯㓭䏦Ṅ僅方値㵛廘䕂奜墾濕⽦ḽ䏦廗ỉ
奜墾⌻ᶢ方値Ṅ㽸⽦Ḛヾ㇅⩘䕂↓Ḛ濕䣉ⷍ奜墾䕂䖬䕂
⫯㓭◦㨢Ʋ
-DYD⚵圊僅弉Ḛ
-DYD䄆㗪㨵䣉
-DYD奜墾储-'.䄆㗪ᶣṄ䱑㨵
ᵄ壯⟘㧟㖲㑮濕ᴝ◦⚸㗪䣉ⷍ⵩
ᳫ❜⅞ᵄ⟥悍䕂朜⃣⍊⠕ᶴƲ⹜
-6(摉⢉濕-DYD奜墾䕂婈↓
䏯-DYD&RPPXQLW\3URFHVV
濑-&3濒䩟䋄濕-&3ḽ䏦-DYD塍䪂
妉㬀濑-DYD6SHFLILFDWLRQ
5HTXHVWV濕-65V濒Ṅⶸ娮⍊⩘乧
⫋-DYDⴱ⊮‥⩷䕂㑮❜⍊Ậ㏷Ʋ
ⅎ⮠凮⛽䈮Ὥ㹷 (䶔⟡䙥䦸)Ɲhttps://zh.wikipedia.org/wiki/Java%E7%89%88%E6%9C%AC%E6%AD%B7%E5%8F%B2#Java_SE_8
摉䔺-DYDヾ⺃曆䕂㯿䣉
• ᳉巇Ʊ⩇垛-'.
• 士⩘3DWK䍮❁婈㐶 妉⊁仁奰⛀亿⳩㎋Ḛ㨣椝 • ḽ䏦䯒㑅⨕㥒㜆㍮⪩ℭ㥒⋋ MDYD䕂䣉ⷍ
• ᴝ⊭ḽ䏦6XEOLPHƱ1RWHSDG䨇‴ᶔ㑅⨕䲦巭┦
• ᴝ⊭ḽ䏦(FOLSVHƱ,QWHOOMƱ1HWEHDQVƱ9LVXDO&RGH䨇,'(
㐲⋆摉䔺ⲣ‵
• 妉⊁仁奰⛀亿⳩䕂弶䏦 • 䲦娭僅⚵圊
• ⹜$3,6SHFLILFDWLRQᳫ㚣夠ヾ斾䕂$3,ᴉ⅝侻
2UDFOH-'.YV2SHQ-'.
• ‧仃ᴉ摑㭐㗇䗝㨡䕂ㄾ圑Ⲭ⃣濕▞㽸悛⫋2UDFOH-'.㢉
ⶸ弌䣉ᴝ㓭⚸㑺2SHQ-'.䕂˛
• ◦⻥侻㑷映濕2UDFOH◦暽ょ侻⅙⍊-90⻥侻㑷映堿⠻⹕⟘Ʋ
䏯㑺‴⫋ᶿ㠫⩠ヴ䕂悋塔濕⩁㖲⅞悋塔䤧⩘⻥Ʋ
• 䖶㪒ᴉ᳉濕2SHQ-'.⫅㖲曹䳿◮䔺ⳁ䄆㗪Ʋ▞㨢冣弅⃮᳋䤧
⩘䕂␍朊Ʋ㛷㎘䟼乢⊋栉⊭ᶣ∱㔀䈰⹕墡㬸Ʋ
᳉巇Ʊ⩇垛-'. IRUZLQGRZV • 2UDFOH-DYD6(᳉巇 斾堿䔹‣㗁⎟ • KWWSVZZZRUDFOHFRPWZMDYDWHFKQRORJLHVMDYDVHMG
NDUFKLYHGRZQORDGVKWPO
• ㄼ⃮MGN[[BZLQGRZV[BELQH[H
• ᳉巇⹊斗㎈∱⊭⩇垛
• 2SHQ-'.-DYD6(᳉巇
• KWWSVZZZRSHQORJLFFRPRSHQMGNGRZQORDGV
᳉巇Ʊ⩇垛-'. IRUPDF • 2UDFOH-DYD6(᳉巇 斾堿䔹‣㗁⎟ • KWWSVZZZRUDFOHFRPWZMDYDWHFKQRORJLHVMDYDVHMG
NDUFKLYHGRZQORDGVKWPO
• ㄼ⃮MGN[[BRV[[BELQGPJ
• 2SHQ-'.-DYD6(᳉巇
• KWWSVZZZRSHQORJLFFRPRSHQMGNGRZQORDGV
2SHQ-'.䍮❁婈㐶IRUZLQGRZV
2SHQ-'.䍮❁婈㐶IRUZLQGRZV
• RSHQORJLFRSHQMGNZLQGRZV[PVL
2SHQ-'.䍮❁婈㐶IRUZLQGRZV
2SHQ-'.䍮❁婈㐶IRUZLQGRZV
2SHQ-'.䍮❁婈㐶IRUZLQGRZV
• 㳪夤
• FPG ⌻ᶢ㊎䟸⨕ • 巶‣MDYDYHUVLRQ
2SHQ-'.䍮❁婈㐶IRUPDF
• ⡀ḓ◦0DF᳈札䟸敯咍㥒㜆⍊㥒㜆⟼"
• KWWSVEORJWUHQGPLFURFRPWZ"S • 摉␝EDVKBSURILOH
• 㑺㥒⫼⅞‣
䕌... JAVA_HOME=/Library/Java/JavaVirtualMachines/openlogic-openjdk-11.jdk/Contents/Home
PATH=$JAVA_HOME/bin:$PATH:
export JAVA_HOME
export PATH
䲦娭僅⚵圊
• -DYDᶣℋ
• ḽ䏦MDYDFⲣ‵䣉ⷍ䲦娭≝⢉䝺㽸 FODVV㥒㜆
• ḽ䏦MDYDⲣ‵䣉ⷍ弉圊Ʊ㳪夤-DYD䣉ⷍ㓭⋤䧤⋆⅝侻
• MDYDF+HOOR:RUOGMDYD
• MDYD+HOOR:RUOG
• -DYDᴉ⹊
• 䖲㉣ḽ䏦MDYDⲣ‵䣉ⷍ䲦娭僅⚵圊Ʋ
• MDYD+HOOR:RUOGMDYD
廍弌ħYHUERVH塾⪝䖲㉣廍弌≝⢉䝺⚵圊
僅䲦娭⹊⁋⚵圊
䛛㎌Ọ MDYD⑤ị⟞堳⎆⦲䢣˛暽䄝䛲ᷱ⎢㘖
䛛㎌⟞堳⎆⦲䢣㩻ƏἭ⯍暂ᷱẴ㘖ℯ䷏孖ḍ
㉱䷏孖㩻⬿⅌姿㆝檻Ə䄝⽳䔘&ODVV/RDGHU
廰⅌⽳⟞堳˛
MDYDħYHUERVH+HOOR:RUOGMDYD
㘩
敺
ὦ
䔏
廪
⤁
MDYDF⑤ị⯮⎆⦲䢣MDYD㩻䷏孖ㇷFODVV㩻⽳ⅴỌMDYD⑤ị
⟞堳˛
MDYDF+HOOR:RUOGMDYD
MDYDħYHUERVH+HOOR:RUOG
㘩
敺
ὦ
䔏
廪
⯸
㮦⿍濊
• 冣+HOOR:RUOGFODVVⲰ⨖◦濕ḽ䏦
MDYD+HOOR:RUOGMDYD⚵圊㔀㗁䔺䏝
• HUURUFODVVIRXQGRQDSSOLFDWLRQFODVVSDWK+HOOR:RUOG
• 曆⫅+HOOR:RUOGFODVV⃨攢
• ᳋㿴⫯䖲㉣ḽ䏦
• MDYD+HOOR:RUOG ᳋⅞‣MDYDℭ㥒⋋ API Specification
• https://docs.oracle.com/en/java
/javase/11/docs/api/index.html
䧪ᵊ⛀濕壹墡Ʊ宅㑗Ʊ婈㐶
⑬
㡀妔
•⑬
䖬㣗
• ⡀ḓ㍮⪩壹墡
• ⃕₸-DYD䣉ⷍ奜墾䕂 ỉ⚸㗪♉〉
• 婈㐶䕂⩡⌈僅ḽ䏦
• ḽ䏦㔇∅濑3URPRWLRQ濒僅帇♉濑&DVWLQJ濒
㽸䣉ⷍ⅞‣壹墡
• 䣉ⷍ奜墾䕂䖬䕂㓭◦僅方値㵛廘
• ㍮⪩䣉ⷍ䕂㓭ᵸ朜濕⽦ᴝ⺃曆䗉む⽦䣉ⷍ䕂⿍◔濕⺃堿
䕂㔀ỗ濕⽦⊭ᶣ㽸⽦䕂䣉ⷍ⅞‣奨㓌
䧲⻶⟡㜓ᷱ恫㘖ạ桅✏䶔
孞䙫
壹墡䕂䰎㢉
• ⑬圊壹墡濑6LQJOHOLQHFRPPHQW濒
• ά䰯壹墡 7UDGLWLRQDOFRPPHQW • 㑅ᶴ壹墡濑'RFXPHQWFRPPHQW濒
壹墡䕂䰎㢉
• ⑬圊壹墡
public class SingleCommentDemo {
public static void main(String[] args) {
// 栗䣢Hello炰World炰
System.out.println("Hello炰World炰");
}
}
壹墡䕂䰎㢉
• ά䰯壹墡
• ⊆䣯Ƽ⟘圊ƽ壹墡
/*ₚ䵘娢姊
⏴䨘Ɲ䬓ᷧῲJava䧲⻶䷛侹
䛕䙫Ɲ✏垉⸼ᷱ桖䤡HelloƄWorldƄ
*/
public class TraditionalCommentDemo {
public static void main(String[] args) {
System.out.println("HelloƄWorldƄ");
}
}
壹墡䕂䰎㢉
• ά䰯壹墡㓭ᶣ Ḛ㽸壹墡摉⢉ᶣ弅⃮ Ḛ㽸壹墡䰎㘝
• ᳋⊭ᶣ䏦Ⲡ䅾濑1HVWHG濒䕂㑷ⷍṄ㍮⪩ά䰯壹墡
/*
⎵䧙烉䫔ᶨᾳJava䦳⺷䶜佺
/*
䚖䘬烉⛐坊ⷽᶲ栗䣢Hello炰World炰
*/
⤁棿䙫挖媋
*/
姣妋⇘忁怲⯙䳸㜇Ḯ
壹墡䕂䰎㢉
• 㑅ᶴ壹墡濑'RFXPHQWFRPPHQW濒
• 㑅ᶴ壹墡⺃曆ᶣ㑚䲘⍊‧ỉ㓝唝摉⢉濑 濒濕今䏯Ჾ
ỉ㓝唝⍊Ჾỉ㑚䲘䰎㘝濑 濒Ʋ
• 咇䏯-'.‥攂䕂ⲣ‵MDYDGRFṄ䏠䏝㑅ᶴƲ
• 䪂ṉ濣MDYDGRFħHQFRGLQJXWI+HOOR:RUOGMDYD
壹墡䕂杦㛺
int numberOfStudent; // ⭟䔆䙫⭟噆
public class HelloWorld {
public static void main(String[] args) {
...
} // main 䴷㝆
} // HelloWorld䴷㝆
壹墡䕂杦㛺
public class HelloWorld {
public static void main(String[] args) {
// System.out.println("Hello炰烎");
System.out.println("World炰烎");
}
}
ἧ䓐姣妋姣㴰ᶵ
゛➟埴䘬䦳⺷䡤
壹墡䕂杦㛺
public class HelloWorld {ἧ䓐姣妋䘬⎎ᶨ䧖㕡⺷
public static void main(String[] args) {
/* System.out.println("Hello炰烎");
System.out.println("Good炰烎");
....ᶨ⣏➮䦳⺷䡤
*/
System.out.println("Hello炰烎");
}
}
ὦ䔏娢姊娢択ᷧ⯴䧲⻶䢣
壹墡䕂杦㛺
/***************************************
* Variable Declaration Section
*
* 嬲㔠⭋⏲⋨➇
*
****************************************/
奋娖宅㑗♉〉
• 㐲㐶♉〉濑,QWHJUDOW\SH濒
• E\WHƱVKRUWƱLQW⍊ORQJ
• 㰬涜㐶♉〉濑)ORDWLQJSRLQWW\SH濒
• IORDWƱGRXEOH
•⨕
♉〉濑7H[WXDOW\SH濒
• FKDU
• 彍巭♉〉濑/RJLFDOW\SH濒
• ERROHDQ
㐲㐶⚸㗪♉〉
✳ン
攟⹎
byte
8 bits
short
16 bits
䭬⚵
-27⇘27-1
炷-128⇘127ℙ256ᾳ炸
-215⇘215-1
炷-32,768⇘32767ℙ65,535ᾳ炸
int
32 bits
-231⇘231-1
炷-2,147,483,648⇘2,147,483,647ℙ
4,294,967,296ᾳ炸
long
64 bits
-263⇘263-1
炷-9,223,372,036,854,775,808⇘
9,223,372,036,854,775,808
ℙ18,446,744,073,709,557,616ᾳ炸
䭬ἳ
2
-114
2
-32699
2
147,334,778
2
-2,036,854,775,808L
1L
㐲㐶⚸㗪♉〉
• 䖲㉣◦䣉ⷍᳫ⪩᳉Ჾỉ㐶Ỻ濕夰㐶Ỻ䣯ᴉ㽸⨕映Ỻ
濑/LWHUDOYDOXH濒
• 䲦娭┦㗁储↓㽸夰㐶Ỻ怋䰤壖ぴ榒䤸摑
• ᶣ㐲㐶Ỻ㽸ṉ濕◦Ჾ僪⾃㭿᳉⡀㙚⽦⪩᳉Ჾỉ㐲㐶濕
䲦娭┦⫅储↓士⩘㽸LQW♉〉
• ṉ⡀⪩᳉/䕂夯濕℅䲦娭┦㗁㽸夰㐶Ỻ怋䰤ORQJ
♉〉䕂䤸摑
㐲㐶⚸㗪♉〉
Ữ㗗⤪㝄ぐ䘬䦳⺷ᷕ㑘⮓Ḯẍᶳ䘬䦳⺷䇯㭝烉
System.out.println(2036854775808);
⇯ぐ⛐䶐嬗㗪㚫䘤䓇ẍᶳ䘬拗婌妲〗烉
integer number too large: 2036854775808
System.out.println(2036854775808);
^
1 error
㐲㐶⚸㗪♉〉
㐲㐶⚸㗪♉〉
ẍᶳ䘬䦳⺷䡤䇯㭝㚫⛐坊ⷽᶲ栗䣢10忚ỵ䘬255ˣ15冯3烉
System.out.println(0xFF);
System.out.println(017);
怙ἴ
怙ἴ
System.out.println(0b011);
怙ἴ
㰬涜㐶⚸㗪♉〉
✳ン
攟⹎
䭬ἳ
float
32 bits
99F
-327,456,99.01F
4.2E6F炷ⶍ䦳㧁姀姀䁢4.2*106)
double
64 bits
-1111
2.1E12
999,701,327,456,99.999
⨕
⚸㗪♉〉
• FKDU⚸㗪♉〉㓭䏦Ṅ῰⨖ELWV䕂⑬Ჾ⨕ • 8QLFRGH䝺
• ◦-DYD䣉ⷍ奜墾ᳫ濕⨕ ⺃曆ḽ䏦⑬ⷓ唝ㆪḍ System.out.println('Y');
⨕
⚸㗪♉〉
• ⊭ᶣḽ䏦8QLFRGH䲦䝺䕂㑷ⷍṄ㇅⩘⨕
?X摉⢉᳤㇅⩘⨕ 䲦䝺∱⊭
System.out.println('\u0048');
H
System.out.println('\u0065');
System.out.println('\u006C');
System.out.println('\u006C');
System.out.println('\u006F');
e
l
l
o
濕⊨堿ᶣ
岱俩⨕ • ハ⾱堿䕂FKDUVLQJOH • 方値奋㽸FKDUVLQJOH • 墡㬸㑷㮓FKDUVLQJOH ? 怀㨊㋮⮁ ⬾K㘖挖䙫
⏚恱⤁ᷧῲ▕⻼噆
奨䔏?㋮⮁巚儒
岱俩⨕ ⫿⃫
䭬ἳ
\\
⍵㕄䶂
\'
╖⺽嘇'
\"
暁⺽嘇"
\uxxxx
ẍ16忚ỵ㔠㊯⭂Unicode⫿⃫廠↢
\dxxx
ẍ8忚ỵ㔠㊯⭂Unicode⫿⃫廠↢
\b
Ὰ徨ᶨᾳ⫿⃫
\f
㎃枩
\n
㎃埴
\r
㷠㧁䦣军埴椾
\t
嶛㟤(ᶨᾳTab挝)
彍巭⚸㗪♉〉
• ERROHDQ♉〉㗇‧䣬㐶ỺWUXHピIDOVH
• ょ䏦㑺㝛ᶴ弉䩕㔀濕弉䩕ⷍ䕂䰎㙚⊭㽸WUXHピ
IDOVH
⩡⌈Ʊḽ䏦婈㐶
わ曧奨ᷧῲ䩡敺ƏἭ恫ᷴ䟌怺奨㔥Ẃầ溣Ƅ
⩡⌈Ʊḽ䏦婈㐶
孱㕟⮊␱䂡わῄ䕀姿㆝檻䩡敺
⩡⌈Ʊḽ䏦婈㐶
• ⩡⌈Ჾỉ壖ぴ榒䤸摑
• ⌈䚣䲦娭┦廗ỉ䤸摑⫅῰⨖䕂宅㑗㓭ᵾ浺♉〉
• 廗ỉ壖ぴ榒䤸摑㗁㗇ỉ⋋䣯
• 婈㐶䕂⋋䣯㗇⟥⫍⪩ᴉ⃄
婈㐶䕂⩡⌈僅ḽ䏦
⛐㕡㱽ᷕ忚埴嬲㔠⭋⏲䘬婆㱽⤪ᶳ烉
type identifier = [value];
int ageOfStudent = 18;
char sexOfStudent = 'M';
⭋⏲᷎㊯⭂ῤ
婈㐶䕂⩡⌈僅ḽ䏦
• ᳋⊭ᶣ䖲㉣⊔䏦婈㐶䕂Ỻ濕䲦娭┦㗁䔺䏝懭奢壈⼭
int ageOfStudent; // ⭋䣢嬲㔠Ữᶵ㊯⭂ῤ
System.out.println(ageOfStudent); // 栗䣢嬲㔠
わ恫㱹㋮⮁‣䵍⭪⑉
㔇∅濑3URPRWLRQ濒僅帇♉濑&DVWLQJ濒
⛐⭋⏲嬲㔠᷎㊯⭂ῤ㗪炻忁㧋⮓ᶵ㚫㚱Ṩ湤⓷柴ˤ
int age = 18;
double score = 90.5;
⤪㝄ぐ忁湤⭋⏲䘬娙炻䶐嬗☐㚫↢䎦拗婌妲〗烉
int age = 2036854775808;
float score = 90.5;
㔇∅濑3URPRWLRQ濒
• ⡀㙚婈㐶䕂宅㑗♉〉‴῰⨖䪂○㪒㇅⩘䕂㐶Ỻᴉ宅
㑗♉〉㽸⟥㔀濕℅㗁䔺䏝㐶Ỻ♉〉㔇∅䕂↓Ḛ
• 䫟⑬䕂奨濕⫯㓭㐶Ỻ䕂♉〉㗁储↓㊎㓅㽸䧤⋆婈㐶
ᴉ宅㑗♉〉
long money = 10;
帇♉濑&DVWLQJ濒
• ⡀㙚㐶Ỻ䕂宅㑗♉〉ᴉ῰⨖䪂○㪒婈㐶䕂宅㑗♉〉
㽸⟥㔀濕䏯㑺≝㐶Ỻ㿟㮓⫅ヾ㗇䕂⩷悍῰⨖傱婈㐶
ᳫ濕▞今䲦娭┦㗁㊎₸懭奢壈⼭
long money = 10;
int smallMoney = money;
possible loss of precision
found : long
required: int
int smallMoney = money;
帇♉濑&DVWLQJ濒
• ⡀㙚⽦䝸⩘堿⫅宅㑗♉〉⟥䕂Ỻ㇅⩘䰤宅㑗♉〉⫍
䕂婈㐶濕℅⽦⺃曆㓌䝸廰圊帇♉濑&DVWLQJ濒䕂↓
Ḛ
• ḽ䏦ㆪ唝 ⇃ㆪ䖬㣗宅㑗♉〉䕂撚戳⨕
long
money = 10;
int smallMoney = (int) money;
double pi = 3.14159;
float piAlso = (float) 3.14; // 廱✳䁢float
帇♉濑&DVWLQJ濒
䧪᳇⛀濕-DYD弉䩕⨎
⑬
㡀妔
•⑬
䖬㣗
• 㐶⨶䖶撚䕂䩕圑弉䩕⨎
• 㬸䨔䖶撚䕂撚ẀƱ㝛ᶴ弉䩕⨎
• 唓䋄㐶ḋ彍巭䕂ḋ 弉䩕⨎
• 䫟ṽ⪩㮓䕂㇅⩘弉䩕⨎
䩕圑弉䩕⨎
• 㣗㵔㐶⨶弉䩕⨎
• 弜❜僅弜㳙弉䩕⨎
• 䩕圑弉䩕僅㔇∅Ʊ帇♉䕂␍朊
+˚-˚*˚/
㣗㵔㐶⨶弉䩕⨎
䓐徼
忳䬿⫸
䭬ἳ
≈㱽
+
num1 + num2
劍num1䫱㕤10ˣnum2䫱㕤2炻忳䬿䳸㝄䫱㕤12ˤ
㷃㱽
-
num1 – num2
劍num1䫱㕤10ˣnum2䫱㕤2炻⇯䳸㝄䫱㕤8ˤ
Ḁ㱽
*
num1 * num2
劍num1䫱㕤10ˣnum2䫱㕤2炻⇯䳸㝄䫱㕤20ˤ
昌㱽
/
num1 / num2
劍num1䫱㕤10ˣnum2䫱㕤2炻⇯䳸㝄䫱㕤5ˤ
㧉㔠炷检昌炸
%
num1 % num2
劍num1䫱㕤31ˣnum2䫱㕤6炻⇯䳸㝄䫱㕤1ˤ
㣗㵔㐶⨶弉䩕⨎
• ⡀㙚堿⫅弉䩕䕂䰎㙚㇅⩘䰤㚎婈㐶濕
℅ḽ䏦㇅⩘弉䩕⨎濑Assignment operator濒=
x = 10;
ℯ嘼䏭 + 怲䭾⬷
int
int y = 5;
int sum = x + y;
System.out.println(sum);
ⅴ嘼䏭 = 怲䭾⬷
弜❜僅弜㳙弉䩕⨎
• 弜❜弉䩕⨎
int age = 10;
age = age + 1;
System.out.println(age); // 栗䣢11
ὦ䔏 ++ 恅⢅怲䭾⬷
int age = 10;
age++;
System.out.println(age); // 札䟸11
弜❜僅弜㳙弉䩕⨎
• 弜㳙弉䩕⨎
int age = 10;
age--;
System.out.println(age); // 栗䣢 9
弜❜僅弜㳙弉䩕⨎
∴何⯒㲼
int age = 10;
++age;
System.out.println(age); // 栗䣢11
--age;
System.out.println(age); // 栗䣢10
弜❜僅弜㳙弉䩕⨎
int age = 10;
int var = ++age;
䚠䔞㕤烉
age = age + 1;
var = age;
int age = 10;
int var = age++;
䚠䔞㕤烉
var = age;
age = age + 1;
䩕圑弉䩕僅㔇∅Ʊ帇♉䕂␍朊
System.out.println(10 / 3);
System.out.println(10.0 / 3);
䴷㞃桖䤡3Ƅᷴ
㛪␎Ƅ
3.3333...ƢƄ
䩕圑弉䩕㔀宅㑗♉〉䕂㔇∅
䩕圑弉䩕㔀宅㑗♉〉䕂帇♉
• ⽦⊭ᶣ⫅㚎ỉ㐶Ỻ廰圊帇♉濕Ƽㄉ↓ƽ⫅ᴉ㔇∅㽸
巁⟥䕂宅㑗♉〉
• ḽ䏦ㆪ唝 ᳤㇅⩘帇♉䕂䖬㣗♉〉撚戳⨕
• System.out.println((double) 10 / 3);
䩕圑弉䩕
䩕圑弉䩕䕂悋涜⫯◦㑺濣
*㉊
㉊㊟弉
㉊㊟弉䩕䕂Ῠ
㊟弉
曄ⵍ
* 㮦⿍宅
㮦
㮦⿍宅㑗♉〉
撚Ẁ弉䩕⨎Ʊ㝛ᶴ弉䩕⨎
• 撚Ẁ弉䩕⨎濑5HODWLRQDORSHUDWRU濒
• 㓭䏦Ṅ㪒巁‧ỉ㐶Ỻ濕᳤⃢⩘‧仃ᴉ摑䕂⟥⫍撚Ẁ
• 㝛ᶴ弉䩕⨎濑&RQGLWLRQDORSHUDWRU濒
• 婑⽦⫋‧ỉ㝛ᶴ廰圊Ƽ᳤᳒
ƽƱƼピ__ƽ䨇⃢㑵
撚Ẁ弉䩕⨎
䉨㱩
忳䬿⫸
䭬ἳ
䚠䫱
==
int i = 1; System.out.print( i == 1 );
ᶵ䫱
!=
int i = 2; System.out.print( i != 1 );
⮷㕤
<
int i = 0; System.out.print( i < 1 );
⮷㕤䫱㕤
<=
int i = 2; System.out.print( i <= 1 );
⣏㕤
>
int i = 1; System.out.print( i > 1 );
⣏㕤䫱㕤
>=
int i = 1; System.out.print( i >= 1 );
撚Ẁ弉䩕⨎
㝛ᶴ弉䩕⨎
忳䬿
忳䬿⫸
䭬ἳ
⤪㝄ᶨᾳ䉨㱩冯⎎ᶨ䉨㱩
⎴㗪䘤䓇
᷎ᶼ(and)
&&
int i = 2 ; int j = 8 ;
(( i < 1 ) && ( j > 6))
⤪㝄ᶨᾳ䉨㱩ㆾ⎎ᶨ䉨㱩
℞ᷕᶨᾳ䘤䓇
ㆾ(or)
||
int i = 2 ; int j = 8 ;
(( i < 1) || ( j > 10))
朆
朆(not)
!
int i = 2 ; int j = 8 ;
( ! ( i < 3 ))
ḋ
弉䩕⨎
忳䬿⫸
㑵ἄ
䭬ἳ
&
op1 & op2
⮯op1冯op2䘬㔠ῤ徸ỵ⃫忚埴And忳䬿
|
op1 | op2
⮯op1冯op2䘬㔠ῤ徸ỵ⃫忚埴Or忳䬿
^
op1 ^ op2
⮯op1冯op2䘬㔠ῤ徸ỵ⃫忚埴Xor忳䬿
~
~op1
⮯op1㔠ῤ⍾℞墄㔠
DQG 弉䩕⨎
op1
op2
䳸㝄
0
0
0
0
1
0
1
0
0
1
1
1
_ RU 弉䩕⨎
op1
op2
䳸㝄
0
0
0
0
1
1
1
0
1
1
1
1
A [RU 弉䩕⨎
op1
op2
䳸㝄
0
0
0
0
1
1
1
0
1
1
1
0
a垚㐶弉䩕
• LQW♉〉䕂ḋ
Ỻょ夰㓭
• • 䱑垚㐶弉䩕⹊婈ノ
• ḋ䢹弉䩕⨎
弉䩕⨎
⋋䣯
䪂ṉ
Ḛ䏦
䭾㕟ⷍ⹚䧢
RSRS
⯮RS䙫ἴKⷍ䧢RSῲ▕ἴƏ⏚恱
壃ᷱ
!!
䭾㕟⏚⹚䧢
RS!!RS
⯮RS䙫ἴK⏚䧢RSῲ▕ἴƏⷍ恱
壃ᷱ⎆Ὥ㛧ⷍ恱䙫ἴK‣
!!!
恶弖⏚⹚䧢
RS!!!RS
⯮RS䙫ἴK⏚䧢RSῲ▕ἴƏⷍ恱
壃ᷱ
ḋ䢹弉䩕⨎
⯮㕟‣怙堳ⷍ䧢
㇅⩘弉䩕⨎
忳䬿⫸
䭬ἳ
ἄ䓐
+=
op1 += op2
op1 = op1 + op2
-=
op1 -= op2
op1 = op1 - op2
*-
op1 *= op2
op1 = op1 * op2
/=
op1 /= op2
op1 = op1 / op2
%=
op1 %= op2
op1 = op1 % op2
&=
op1 &= op2
op1 = op1 & op2
|=
op1 |= op2
op1 = op1 | op2
^=
op1 ^= op2
op1 = op1 ^ op2
<<=
op1 <<= op2
op1 = op1 << op2
>>=
op1 >>= op2
op1 = op1 >> op2
>>>=
op1 >>>= op2
op1 = op1 >>> op2
弉䩕⨎䕂Ῠ
曄ⵍ
• -DYD弉䩕⨎㗪嵩ᴝ㗇储ⲯ䕂弉䩕Ῠ
僇Ჾỉ䫟⑬䕂ṉ⨎濣
曄ⵍ濕桔
• [ 㬀[ 濨
• ⹕₸䕂[Ỻ㽸濕今᳋㓭Ʋ廗㓭▞㽸ハỏ徻䚣弑
攢⹊⅞㳙䕂撚ẀƲ
ᴖ
弉䩕⨎䕂Ῠ
曄ⵍ
• 冣⫅朊䖬婈⹕埅斚Ჾ涜
• [ !ħ__! ħ
IDOVH
• 廗堿⡀ḓ壆䩕⌠濨濨
弉䩕⨎䕂Ῠ
曄ⵍ
• 㗇Ჾ⫋◑ㆪⷥ䕂弉䩕⨎
• 弜❜⊈弜㳙弉䩕⨎
• ᴖ㮓僅攢㮓弉䩕⨎㓭䏯Ⲥ今⊱Ἐ壆䩕
• ⅞㮓僅㳙㮓弉䩕⨎㓭䏯Ⲥ今⊱Ἐ壆䩕
• Ჾ㣡Ῠ 㧈䕂弉䩕⨎◦Ჾỉ攱庮⊣ᳫ廡䵊◮₸䊼濕
℅⩁ỏ徻㓭䏯Ⲥ今⊱Ἐ壆䩕
弉䩕⨎䕂Ῠ
Ῠ
曄ⵍ
曄ⵍ
弉䩕䧤
㨡 孞 a
⅞ 㳙 !!!!!
!! LQVWDQFHRI
A
_
!!
__
"
_ A a !! !!! !!
⇯劍
䚳䡘
⍇⇘
忳䚠
䬿⎴
⺷䘬
⃒⃰
䡘⃰
⇘㪲
婘ῤ
炻
婘
⯙
⃰
!
⃒
⃰
㪲
ῤ
崲
⮷
堐
䣢
崲
⃰
塓
忳
䬿
>@
弉䩕⨎䕂Ῠ
曄ⵍ
• 墡㙎濣
• [ !ħ__! ħ
•
䩕 ƱƱƱ䕂䰎㙚
• !__! IDOVH
• ⁋䩕Ʊ!Ʊ! 䕂䰎㙚
• WUXH WUXH__WUXH
IDOVH
• ⁋䩕WUXH WUXH䕂䰎㙚
• WUXH__WUXH
• ⁋䩕WUXH
IDOVH
IDOVH䕂䰎㙚
• WUXH__IDOVH
• [ WUXH
Module 1, slide 35 of 24
IDOVH
䧪▙⛀濕-DYD㬸䨔㉥⃴
⑬
㡀妔
•⑬
䖬㣗
• ⊔⹕ḽ䏦仃䕂巶‣
• ⶸ䦉LI⍊LIHOVH攱庮
• ḽ䏦VZLWFK攱庮
⊔⹕ḽ䏦仃巶‣
• ḽ䏦㣗㵔巶‣⊔⹕宅㑗
• ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
java.util.Scanner scanner =
new java.util.Scanner(System.in);
⻡䪲ᷧῲ
Scanner⯍ὲ
ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
import java.util.Scanner;
...
Scanner scanner = new Scanner(System.in);
⻡䪲ᷧῲ
Scanner⯍ὲ
ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
• ḽ䏦⋂䣬QH[W;;; 㑷㮓濑0HWKRG濒⊔⹕⋂䣬♉
〉䕂宅㑗
• QH[W,QW ⊭⫅ḽ䏦仃巶‣帇㊙㽸LQW♉〉
• QH[W6KRUW ⊭⊔⹕VKRUW♉〉䕂巶‣
• QH[W'RXEOH ⊭⊔⹕GRXEOH♉〉䕂巶‣
• QH[W ⊭⊔⹕6WULQJ♉〉䕂巶‣
• QH[W/LQH ⊭⊔⹕Ჾ⃕宅㑗
ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
• 斄㕤6FDQQHUἧ䓐䘬㕡㱽婒㖶炻⎗㞍娊䶂ᶲ$3,㔯
ẞ
• KWWSVGRFVRUDFOHFRPHQMDYDMDYDVHGRFV
DSLMDYDEDVHMDYDXWLO6FDQQHUKWPO
ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
Scanner scanner = new Scanner(System.in);
System.out.print("廠ℍ㔜㔠烉");
int input1 = scanner.nextInt();
System.out.print("廠ℍ㴖溆㔠烉");
double input2 = scanner.nextDouble();
System.out.print("廠ℍⶫ㜿㔠烉");
boolean input3 = scanner.nextBoolean();
ḽ䏦MDYDXWLO6FDQQHU⊔⹕巶‣
• ⡀㙚6FDQQHU㿟㮓⫅ḽ䏦仃䕂巶‣帇㊙㽸㇅⩘䕂
宅㑗♉〉濕℅䣉ⷍ⫯㗁䔺䏝
MDYDXWLO,QSXW0LVPDWFK([FHSWLRQ懭奢
ⶸ䦉LI⍊LIHOVH攱庮
LI䰎㢉
if ( boolean_expression )
statement;
乣乳⽧ᷲ⟞堳䧲⻶
if(input % 2 == 0) // ⤪㝄检㔠䁢0
System.out.println(input + " 㗗„㔠");
if(input % 2 != 0) // ⤪㝄检㔠ᶵ䁢0
System.out.println(input + " 㗗⣯㔠");
LI䰎㢉
壮⏯晚志⏌Ƌ&RPSRXQGVWDWHPHQWƌƝ
if ( boolean_expression ) {
code_block;
...
} LI䴷Ⱕ
乣乳⽧ᷲ⟞堳䧲⻶
if(input == 36) {
System.out.println("〕╄ぐ䋄⮵Ḯ炰");
System.out.println("㔠ῤ䁢 " + 36);
}
LI䰎㢉
Ⲡ䅾LI䰎㢉
if ( boolean_expression1 ) {
code_block1;
...
if ( boolean_expression2 ) {
code_block2;
} // if䳸
䳸⯦
...
} // if 䳸⯦
// 两临⼨ᶳ➟埴䦳⺷
Ⲡ䅾LI䰎㢉
if ( boolean_expression ) {
code_block_1;
} // if䳸
䳸㥳䳸⯦
else {
code_block_2;
} // else 䳸㥳䳸⯦
// 䦳⺷䓙㬌两临
if(input % 2 == 0) {
System.out.println(input + " 㗗„㔠");
}
else {
System.out.println(input + " 㗗⣯㔠");
}
LIHOVH䰎㢉
抆䅾LIHOVH䰎㢉
if ( boolean_expression_1 ) {
code_block_1;
} // if䳸㥳䳸⯦
else {
if ( boolean_expression_2 ) {
code_block_2;
} // if䳸㥳䳸⯦
else {
code_block_3;
} // else 䳸㥳䳸⯦
} // else 䳸㥳䳸⯦
if ( boolean_expression_1 ) {
code_block_1;
} else if ( boolean_expression_2 ) {
code_block_2;
} else {
code_block_3;
◖Ƅ怀溣⯒⥤
}
₶㷬㥁⤁ṭƄ
抆䅾LIHOVH䰎㢉
if(score >= 90)
System.out.println("⼿A");
else if(score >= 80 && score < 90)
System.out.println("⼿B");
else if(score >= 70 && score < 80)
System.out.println("⼿C");
else if(score >= 60 && score < 70)
System.out.println("⼿D");
else
System.out.println("⼿E(ᶵ⍲㟤)");
抆䅾LIHOVH䰎㢉
⇅⬠侭⎗傥㚫忁湤㑘⮓烉
if ( boolean_expression_1 )
if ( boolean_expression_2 )
⊇ᷱ劘㋓噆 {}
code_block_1;
else
忁湤㑘⮓⯙ᶵ㚫㚱䔹ㄖ烉
code_block_2;
if ( boolean_expression_1 ) {
if ( boolean_expression_2 )
code_block_1;
else
code_block_2;
}
᳇
弉䩕⨎
int remain = input % 2;
System.out.println(input + " 㘖" +
((remain == 0) ? "⁝㕟" : "⤮㕟"));
ḽ䏦VZLWFK攱庮
switch ( variable ){
case literal_value:
code_block;
[break;]
case another_literal_value:
code_block;
[break;]
[default:]
code_block;
}
[ ] 塏䤡婙㋮ị⏖Ọ恟㒮『㒗⯒
ḽ䏦VZLWFK攱庮
ḽ䏦VZLWFK攱庮
switch(level) {
case 10:
case 9:
System.out.println("⽾A");
break;
case 8:
System.out.println("⽾B");
break;
case 7:
System.out.println("⽾C");
break;
case 6:
System.out.println("⽾D");
break;
default:
System.out.println("⽾EƋᷴ⎱㠣ƌ");
}
ḽ䏦VZLWFK攱庮
ḓ㔀ḽ䏦VZLWFK䰎㢉
• ᶹḓ䨇ⷍ䕂㥠㳪
• ⑬Ჾ婈㐶䕂⃢㑵濕⡀PRQWK
• ⃢㑵Ỻ䕂♉〉㽸LQWƱVKRUWƱE\WHƱFKDUピ
6WULQJ
• -DYD摉⢉㏭㊲6WULQJ㥠㳪
㉱ level 䭾⇡Ḳ⽳Ⱈ䛛㎌㮻⯴Ə怆⺍廪⿒
ḓ㔀ḽ䏦VZLWFK䰎㢉 int level = score / 10;
㭷ᶨ㫉悥天慵㕘⍾↢score䘬嬲㔠ῤẍ忚埴㭼⮵烉
if(score >= 90)
System.out.println("⼿A");
else if(score >= 80 && score < 90)
System.out.println("⼿B");
else if(score >= 70 && score < 80)
System.out.println("⼿C");
else if(score >= 60 && score < 70)
System.out.println("⼿D");
else
System.out.println("⼿E(ᶵ⍲㟤)");
switch(level) {
case 10:
case 9:
System.out.println("⽾A");
break;
case 8:
System.out.println("⽾B");
break;
case 7:
System.out.println("⽾C");
break;
case 6:
System.out.println("⽾D");
break;
default:
System.out.println("⽾E(ᷴ⎱㠣)");
}
䧪ᵒ⛀濕-DYD庲◆㉥⃴
⑬
㡀妔
•⑬
䖬㣗
• ZKLOH庲◆僅GRZKLOH庲◆䕂ḽ䏦
• IRU庲◆䕂ḽ䏦
• EUHDN僅FRQWLQXH䕂ḽ䏦
ZKLOH庲◆
• ERROHDQBH[SUHVVLRQ㓭㗁䏠
䏝WUXHピIDOVH䕂弉䩕ⷍƲ◦㪍
Ჾ㧟⚵圊庲◆ᴉℋ徻⺃曆 ⃢
㑵㨢弉䩕ⷍヾ䏠䏝䕂ⳁ㙕Ỻ
• ἅ⡀ERROHDQBH[SUHVVLRQ⃢
㑵₸Ỻ㓭WUXH濕℅㗁悋埅⚵圊
䣉ⷍ䝺⇾✈濑FRGHBEORFN濒
while (boolean_expression) {
code_block;
} // while䳸⯦
// 䦳⺷⽆忁墉两临
ZKLOH庲◆
int count = 0; // 妰㔠䓐
while(count < input) { // ⇌㕟㫉㔠
System.out.println("HelloロWorldロ");
count++; // 㭷㫉➟埴⼴怆⡆
}
ZKLOH庲◆
Ⲡ䅾ZKLOH庲◆
int widthCount = 0;
while(widthCount < width) {
int lengthCount = 0;
while(lengthCount < length) {
System.out.print("@");ℏⰌwhile
lengthCount++;
}
System.out.println(); // ㎃埴
widthCount++;
}
@@@@@@
@@@@
@@@@@@
@@@@
@@@@@@
@@@@
GRZKLOH庲◆
• 冣ERROHDQBH[SUHVVLRQ䕂攱
庮⊣㽸WUXH㔀濕℅
FRGHBEORFN⫯㗁坩⁋㧟⚵圊
• ERROHDQBH[SUHVVLRQ㓭㗁䏠
䏝WUXHピIDOVH䕂弉䩕ⷍƲ◦
⚵圊GR⹊今㪍Ჾ㧟悋㑮⚵圊
庲◆ᴉℋ徻⺃曆 ⃢㑵㨢弉䩕
ⷍ䕂⚵圊䰎㙚Ʋ
do {
code_block;
} while (boolean_expression);
GRZKLOH庲◆
GRZKLOH庲◆
int input = 0;
do {
System.out.print("廠ℍ㔠⫿ㄆ");
input = scanner.nextInt();
} while(input != 30);
System.out.println("〕╄ぐロ䋄⮵Ḯロ");
ḽ䏦IRU庲◆
for徜⚰婆㱽䁢ㄆ
for (initialize ; boolean_expression ; update) {
code_block;
}
IRU庲◆
IRU庲◆
for徜⚰婆㱽䁢ㄆ
for (initialize ; boolean_expression ; update) {
code_block;
}
⮎ἄ䭬ἳㄆ
for(int i = 0 ; i < input ; i++) {
System.out.println("HelloロWorldロ");
}
Ⲡ䅾IRU庲◆
for(int i = width; i > 0; i--) {
for(int j = length; j > 0; j--) {
System.out.print("@");
}
System.out.println(); // ㎃埴
}
㪒巁庲◆䕂䰎㢉
• 䐴悋埅䕂㧟㐶᳋䝸⩘᳤᳒⺃曆 㳪夤庲◆㝛ᶴ㔀濕
⫯ḽ䏦ZKLOH庲◆Ʋ
• 䐴悋埅䕂㧟㐶᳋䝸⩘᳤᳒⺃曆 ⚵圊㚎ỉ↓Ḛ⹊㳪
夤庲◆㝛ᶴ濕⫯ḽ䏦GRZKLOH庲◆Ʋ
• IRU庲◆㪒ZKLOH庲◆㖲䫟㸒⊈⩷㓑摯娾濕≝▞㓭▞
㽸⩁坩士壆ノᶣ⃄摉䕂㐶⨕Ṅ壆㐶Ʋ▞㨢ḽ䏦IRU
庲◆侻⟞ᶣ曎 ⩘乧䕂⚵圊㧟㐶ṄᲾ㨣Ჾ㨣弒ノハ
ỏ室ᵆ⩁ỏ䕂ⲣḚƲ
ḽ䏦EUHDN
while(true) {
count++;
sum += score;
System.out.print("廠ℍ↮㔠(-1䳸㜇)ㄆ");
score = scanner.nextInt();
if(score == -1)
break;
}
ḽ䏦EUHDN
ḽ䏦FRQWLQXH
for(int i = 1; i < 10; i++) {
if(i == 5)
continue;
System.out.println("i = " + i);
}
ḽ䏦FRQWLQXH
EUHDN㋫怋㣗䬢
兯ㆪ唝 { } ⊭⅞ᴝ⊭ᶣ᳋⅞
label1 : {
for(int i = 0; i < 10; i++) {
if(i == 9) {
System.out.println("break label1");
break label1;
}
}
徜⚰ᷳ⼴➟埴");
System.out.println("for徜
} // label1 ⋨⟲䳸⯦
EUHDN㋫怋㣗䬢
FRQWLQXH㋫怋㣗䬢
label1:
for(int i = 0; i < 5; i++){
System.out.println();
label2:
for(int j = 0; j < 5; j++) {
if(j == 3) {
continue label1;
}
System.out.print("\tj = " + j);
}
}
%%!$ '''!$
%'
•
M
Download