?3? Java?????? - PowerPoint PPT Presentation

1 / 184
About This Presentation
Title:

?3? Java??????

Description:

Title: PowerPoint Author: Last modified by: zgz Created Date: 9/10/2001 1:42:38 PM Document presentation format – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 185
Provided by: 4330
Category:
Tags: bubblesort | java

less

Transcript and Presenter's Notes

Title: ?3? Java??????


1
?3? Java??????
2
????
  • 3.1 Java?????
  • 3.2 ????????????
  • 3.3 ???????
  • 3.4 ?????????
  • 3.5 ??
  • 3.6 ??

3
3.1 Java?????
?3-1 Java?????
  • //FileStructure.java
  • package myPackage
  • import javax.swing.JOptionPane //???JOptionPane
  • public class FileStructure
  • public static void main( String args )
  • JOptionPane.showMessageDialog( null,
    "?????Java????!")
  • System.exit( 0 ) // ??????

??????????Welcome.class???myPackage?,
4
3.1 Java?????
  • Java????????????
  • (1)?????(package statement,??)
  • (2)????????(import statements, ??)
  • (3)??????????????

5
Java???
  • ????????AZ,az
  • ????09
  • ?????? ! _(???) -
    lt gt / \
  • . , ( )

6
????
  • ???????Java??????
  • ???????????,?????????????
  • ???????????,???
  • ? ? ??????????????????.
  • ?????????????????.
  • ?123,4.56,A,Hello,ture
  • ????????????,?,-,,
  • ??????????????????.
  • ?( ) ,
  • ??????????????????
  • ??(space)????(TAB??????)?
  • ???(Enter ???????)???????

7
???(Keywords)
abstract break byte boolean catch
case class char continue default
double do else extends false final
float for finally final if
import implements int interface instanceof long
length native new null package
private public protected return switch
synchronized short static super try true
this throw throws thread safe transient
void while
8
???(identifier)
????????????????????????????????
  • Java???(identifier)?????
  • ???????(_)??????
  • ?????????????????(_)???09???
  • ???Java?????
  • ??????????????????
  • ??????????

??????
identifier ,userName , User_Name,
_sys_value, change, 2mail, room, class
??????
9
3.2 ????????????
  • Java?????
  • ??????Java???????????
  • ??????(????)????????????????

10
Java?????
11
??????
???? ??? ???? ????
??? boolean 8 true, false
??? char 16 \u 0000 \u FFFF'
??? byte 8 -128127
??? short 16 -3276832767
?? int 32 -2147483648 2147483647
??? long 64 -2-63263-1
??? float 32 1.40129846432481707e-45 3.40282346638528860e38
???? double 64 4.94065645841246544e-324 1.79769313486231570e308d
12
3.3.1 ??
  • ???????????????????????, ??????

??,123,5.6, 'B'???????
  • Java??????
  • ????
  • ?????
  • ????
  • ????
  • ?????

13
1. ????(byte,short,int,long)
  • ???????????
  • Java??????????
  • ???
  • ???
  • ????
  • ???????????????
  • ??(int)
  • ????(long)??


14
?????????
  • ????? ???09???
  • ?100,-50, 0
  • ????? ?0??????07???
  • ?0123??????83,
  • -011??????-9?
  • ???????0x?0X??????
  • 09,af,AF????
  • ?0x2F????????47?
  • 0x123??????291,
  • -0X12??????-18?

15
????
  • ????????????????
  • ??(int)????32??
  • ?123,-34
  • ???(long)????64?,??????????????L????l?
  • ?-386L,017777l?

16
2.????? (float,double)
  • ?????????????????????
  • ???????????,??
  • ?????????32???,?F?f????19.4F,3.0513E3f,8701.52f
  • ?????????64???,??D?d??????????,
  • ?2.433E-5D,700041.273d,3.1415?
  • ????????????????????

17
?????
  • ??????????????
  • ?0.123, 1.23, 123.0?
  • ????
  • ?0.123e3??0.123103,
  • -35.4E-5??-35.410-5,
  • ??e?E???????,?e?E???????????

18
3.????(char)
  • ??????????????????
  • ????'a','Z','8',''
  • ????'\n' , '\t'
  • '\ddd ' ?????????.
  • '\uxxxx' ?????????,?? Unicode??
  • ? ' 101' ??????????'A'
  • '\u0041' ?Unicode?????'A'
  • ??????????????????????
  • ?Unicode???.
  • Unicode??ASCII??????2???00,
  • ?2????ASCII?

19
?3-2 ????
20
4.?????(String)
  • ?????????????????(??0?)?
  • ???Hello?,
  • ?My\nJava?,
  • ?How old are you? 1234?,
  • ? ?
  • ??
  • ?My? ?name?
  • ??????String????

21
5.???? (boolean)
  • ????true(?)?false(?)?
  • ??????????????
  • If (??) ??1
  • else ??2
  • Java??????????,????????????????????

22
3.2.3 ??
float r, area r4.5 area3.14159rr
  • ??????????????????,
  • ????????????????
  • ?????

byte,short,int,long, float,double,char,boolean
????
???? ???1,???2,???n
?? int num,total double d1
????? ??
23
??
  • ?? ??????

?? int num,total double d1
num
5
6
????? ??
10
total
num5 total2num d1-234.45 num6 K90 NumJ
90num
-234.45
d1
24
??????
???? ??? ???? ????
??? boolean 8 true, false
??? char 16 \u 0000 \u FFFF'
??? byte 8 -128127
??? short 16 -3276832767
?? int 32 -2147483648 2147483647
??? long 64 -2-63263-1
??? float 32 1.40129846432481707e-45 3.40282346638528860e38
???? double 64 4.94065645841246544e-324 1.79769313486231570e308d
25
??
  • ?????? ???????????

?? char a 'a', b double d1,
d20.0 boolean flagtrue
byte b1-128 b1127 byte
b2-129 b2128 //Error!
26
??
  • ???????? ?????????????????

class DynInit public static void main(String
args) double a 3.0, b 4.0
double c Math.sqrt(a a b b)
System.out.println("Hypotenuse is" c)
  • c is dynamically initialized

27
3.2.4 ????
float r, area r4.5 area3.14159rr
float r, area r4.5 areaPIrr
  • ????????????
  • ??????
  • final ???? ??????

?? final double PI3.14159
PI3.1415 //??
??,?????????????,?????????????
28
?3-2 ????????
  • //UseVariables.java
  • public class UseVariables
  • public static void main(String args)
  • final int PRICE30 // ??????
  • long l1234l // ??????????
  • int num,total // ??????
  • float r,v,h 3.5f // ???????,??h??
  • double w 3.1415 // ???????????
  • boolean truth true // ???????????
  • boolean false1 //????????
  •  

29
char c //???????? c 'A'
// ????????????????,????? num10
// ??????? totalnumPRICE
r2.5f //
???????? v3.14159frrh false1 6 gt 7
// ????????? String s"I am a
student" //??????????
????????????
System.out.println(false1) System.out.println("
false1 " false1) System.out.println(" false1
" false1 " \ns s )
30
3.3 ???????
int S-axxbxc boolean lagtb
  • ??? ???????????
  • ?????????????(?-a),?????(?ab) ??????(?e1?e2e3)
  • ????
  • ????? ,?,,/,,,??
  • ????? gt,lt,gt,lt,,!
  • ????? !,,
  • ???? gtgt,ltlt,gtgtgt,,,,
  • ????? ,,?,,/??
  • ????? ?
  • ?? , ,instanceof, () ??

31
???????
  • ???????????(??????????)??????????
  • ????????
  • ??? ab
  • ??? ab
  • ??? ab

?? int a1 c(-abc67)56 i
nt b3 int c0
32
3.3.1 ???????????
  • ????????????????
  • ?????,??????????
  • ?????
  • ?????

33
???????????
???????
??? ?? ? ??
? a b ?a?b????
- ? a - b ?a?b????
? a b ?a?b????
/ ? a / b ?a??b??
?? a b ?a??b?????
34
???????
  • ??
  • (1)??(int, long, short)?????????,float?double???
    ??
  • (2)???????????,???????????,?????????
  • ?? 2/40
  • 2.0/40.5
  • 1353
  • 23.612 //Error

35
???????????
  • ??????????????????

?3-4 ???????
??? ?? ? ????
?? a?a a a 1
-- ?? a--?--a a a - 1
- ??? -a a -a
36
???????
  • ??
  • int x-1
  • x -x

int y ( x ) 3 //x ?2,y?6
int y (x ) 3 //x ?2,y?3
  • ??(,--)???????,?????????? ???????????
  • ??(, -- )??????????,???????????
  • ???????????????

37
??????????
  • ??
  • int x 5
  • int y (-- x ) 3

X?4 Y?12
X?4 Y?9
??? int y (x-- ) 3
X?3 Y?13 Z?9
int z(y)-(-- x)
38
???????????
  • ?3-3????????.
  • ?????application
  • ?????,??????????????????
  • ?????????????????,??????????a?b
  • ?a?b??????????,????????????
  • ?????,??????????,????????????

39
?3-3??????????????
40
  • //ArithmeticOperator.java,
  • import javax.swing.JOptionPane
  • public class ArithmeticOperator
  • public static void main( String args )
  • String input1
  • String input2
  • int a,b
  • int plus,minus
  • input1 JOptionPane.showInputDialog(
    "?????? " )
  • input2 JOptionPane.showInputDialog(
    "?????? ")
  • a Integer.parseInt( input1 )
  • b Integer.parseInt( input2 )
  • plusabminusa-b
  • String s1a " " b " " plus"\n"
  • s1a " - " b " " minus"\n"
  • s1a " " b " " (ab)"\n"
  • s1a " / " b " " (a/b)"\n"
  • s1a " " b " " (ab)"\n"
  • JOptionPane.showMessageDialog( null, s1,
    "??????", JOptionPane.PLAIN_MESSAGE )

41
3.3.2 ???????????
  • Java??????(??-??)
  • ?????????????????
  • ?????????????????????????????????????????????,??
    ??????????

??,i5 //????????5
42
???????????
  • i 1 //?????1
  • ijk1 //?????1 ,i,j,k???1
  • i2(j4) //?????6, j???4,i???6?
  • i(j10)(k2)
  • //?????20, j???10,k???2,i???20?
  • Java?????????????
  • ltopgt

43
? 3-5 ???????
??? ?? ???
x a x x a
- x - a x x - a
x a x x a
/ x / a x x / a
x a x x a
x a x x a
x a x x a
x a x x a
ltlt x ltlt a x x ltlt a
gtgt x gtgt a x x gtgt a
ltltlt x ltltlt a x x ltltlt a
44
???????????
  • ?a3 ??? aa3 xy8 ???
    xx(y8)

45
3.3.3 ???????????
  • ????????????????????
  • ????????????(true?false)

?? int x 5, y 7 boolean b ( x y )

46
?3-6?????
??? ?? ?
?? ab
! ??? a!b
gt ?? agtb
lt ?? altb
gt ???? agtb
lt ???? altb
47
???????????
  • ?3-4 ?????????
  • ?????application.
  • ?????,??????????????????
  • ???????????????????,??????????a?b
  • ???a?b??????????,???????
  • ?????,??????????,????????????

48
?3-4 ??????
49
import javax.swing.JOptionPane public class
RelationOperator public static void main(
String args ) String input1
String input2 int plus,minus
boolean ee input1 JOptionPane.showInputDi
alog( "?????? " ) input2
JOptionPane.showInputDialog( "?????? ") a
Integer.parseInt( input1 ) b
Integer.parseInt( input2 ) ee(a!b)
String s1a " gt " b " " (agtb)"\n"
s1a " lt " b " " (altb)"\n"
s1a " gt " b " " (agtb)"\n"
s1a " lt " b " " (altb)"\n"
s1a " " b " " (ab)"\n"
s1a " ! " b " " ee"\n"
JOptionPane.showMessageDialog( null, s1,
"????????", JOptionPane.PLAIN_MESSAGE )
50
3.3.4 ???????????
  • ????????????????,?????????????
  • ??????????3.7

true false (3gt1) (5gt-4) ! false
51

???????????
???
?3-7 ?????
??? ?? ? ????
! ???? ! x x????,x????
??? x y x,y????????(??)
??? x y x,y????????(??)
?????? x y x,y????????
????? x y x,y????????
????? x y x,y????????
52
???????????
  • ????????????????
  • ?????
  • ??????????,???????????????(??)?????
  • ???????? ?
  • ??????????????,??????????????????,??????????????
    ????

e1 e2
e1 e2
false
53
???????????
??
??int x 3, y 5 boolean b x gt y x
y-- //x?3,y?5,b?false?
boolean b x gt y x y--
//x?4,y?4,b?false?
54
???????????
  • ????????????????????????

?? int x12,y13 float a1,b2 x y a gt
b !(a gt b) x gt y (a3) gt b ! ( x gt a
) true
55
???????????
  • ?3-5 ??????????.
  • ?????application.
  • ?????,??????????????????
  • ???????????????????,???????????a?b
  • ???a?b??????????,???????.
  • ?????,??????????,????????????

????????boolean Boolean.valueOf(String
s).booleanValue()
56
?3-5??????
57
import javax.swing.JOptionPane public class
LogicalOperator public static void main(
String args ) String input1
String input2 boolean a,b boolean
ee input1 JOptionPane.showInputDialog(
"???????? ") input2
JOptionPane.showInputDialog( "???????? ")
a Boolean.valueOf(input1).booleanValue()
b Boolean.valueOf(input2).booleanValue()

58
String s1"" s1a " " b " "
(ab)"\n" s1a " " b " "
(ab)"\n" s1" ! " a " "
(!a)"\n" s1a " " b " "
(ab)"\n" s1a " " b " "
(ab)"\n" s1a " " b " "
(ab) JOptionPane.showMessageDialog( null,
s1, "??????", JOptionPane.PLAIN_MESSAGE )

59
3.3.5 ????
  • ??????????????(bit)???????????,??????????????????
    ,???????.

60
?3-8 ????
61

???????
  • ?????????????????
  • ? 0250000000000010101
  • 0251111111111101010

62
??????
  • ???? 3 5
  • 3 0 0 0 0 0 0 1 1
  • 5() 0 0 0 0 0 1 0 1
  • 3 5 0 0 0 0 0 0 0 1
  • ??
  • ?????0,??????
  • ??? char ??? a ????? 0
  • a a 0376
  • ??????? char c int a
  • c a 0377 //?? a ????

63
??????
  • ?? ?? 3 5
  • 3 0 0 0 0 0 0 1 1
  • 5() 0 0 0 0 0 1 0 1
  • 3 5 0 0 0 0 0 1 1 1
  • ??
  • ?????1,????????? int ??? a ????? 1
    a a 0xff

64
??? ????
  • ???? 071052
  • 071 0 0 1 1 1 0 0 1
  • 052() 0 0 1 0 1 0 1 0
  • 071052 0 0 0 1 0 0 1 1
  • ??
  • ??????(?0??????,?1????)
  • ???? 01111010 ?????
  • 0 1 1 1 1 0 1 0
  • () 0 0 0 0 1 1 1 1
  • 0 1 1 1 0 1 0 1

65
?????
  • ????(ltlt)
  • ???,???0,?????
  • ?? (59) 0011 1011ltlt1 0111 0110 (118)
  • (59) 0011 1011ltlt2 1110 1100
    (236)

byte a59 System.out.println(altlt1)
a59 System.out.println(altlt2)
????? 59ltlt1 59ltlt2
????? -77ltlt1
byte a-17 System.out.println(altlt1)
  • ????1?,??2?

66
?????
  • ????(gtgt)
  • ???,???? ???????0
  • ????????
  • ?? (59) 0011 1011gtgt1 0001 1101 (29)
  • (-69) 1011 1011gtgt1 1101 1101
    (-35)

69??? 01000101 -69?28--69187
-35?28--35221
  • ????1?,??2?

67
?????
  • ????????? (gtgtgt)
  • ???,???? ???0
  • ?? (59) 0011 1011gtgtgt1 0011 1101
  • (187)1011 1011gtgtgt1 0101 1101(93)

????? 59gtgtgt1 187gtgtgt2
int a59 System.out.println(agtgtgt1)
a187 //a-69 System.out.print
ln(agtgtgt1)
  • ??????1?,??2?

68
(77) ??? 01001101 (-77) ??? 10110011 -77ltlt1
1..111100110 (102) -77gtgtgt1 1..111011001
(-39)
????????BitOperator .java
  • import javax.swing.JOptionPane
  • public class BitOperator
  • public static void main( String args )
  • byte a025,b4
  • System.out.println(""a""(a))
  • System.out.println(a""b""(ab))
  • System.out.println(a""6""(a6))
  • System.out.println(a""b""(ab))
  • a59
  • System.out.println(a"ltlt"2""(altlt2))
  • System.out.println(a"gtgt"2""(agtgt1))
  • a-77
  • System.out.println(a"ltlt"1""(byte)(altlt1))
  • System.out.println(a"gtgtgt"1""(byte)(agtgtgt1
    ))

????
69
3.3.6 ?????
  • ???????????
  • e1?e2e3
  • ?? ( )
  • ???
  • ????? instanceof
  • boolean b MyObject instanceof TextField
  • instanceof?????????????????????????

70
???????????
  • ???????????
  • e1?e2e3
  • ??e1 ? boolean ??,e2?e3?????
  • ????
  • ?e1???true,,e2???????
  • ?e1???false,,e3???????

?? y xgt0 ? x -x //?x
max xgty ? x y //?x,y????
71
3.3.7 ???????????
  • ????????????????????????????????????,???????????,
    ???????????????
  • ??????????????????????????

72
3-10 Java???????????
73
Java???????????
  • ??
  • x gt y ! z ??? ( x gt y ) ( ! z )
  • x y z ??? ( x y ) z
  • ! !x ??? !( ! x )
  • a gtgt b 3 ??? a gtgt (b 3)

74
3.3.8 ????????????
  • ????????????????????,?????????
  • Java????????????
  • ?????????????????????????????????,?????????????
  • ??????????????????????????,??????????

??, long lVal1000 int ival200 lValival Doubl
e d1123.5f
75
?????????????
???? ??? ???? ????
??? boolean 8 true, false
??? char 16 \u 0000 \u FFFF'
??? byte 8 -128127
??? short 16 -3276832767
?? int 32 -2147483648 2147483647
??? long 64 -2-63263-1
??? float 32 1.40129846432481707e-45 3.40282346638528860e38
???? double 64 4.94065645841246544e-324 1.79769313486231570e308d
76
????????????
  • ????????????????????????????,?????????????????????
    ??????,????????????????
  • ???????
  • ?
    ?
  • byte short char int float
    double

77
????????????
  • ???????????????????????????
  • ?????????
  • (????)???
  • ????????,???????????????????,????????????????

78
????????????
  • ?3-6?????????

int iVal258 long lVal1000 short sVal12 byte
bVal 2 char cVal 'a' float fVal
5.67f double dVal .1234 lValiVal dVal
fVal //????, lVal???258?dVal???5.67
79
System.out.println(lVal","dVal) bVal(byte)
iVal /??258??????byte???,????258??256???2/
System.out.println(bVal) iVal(int)lVal
//???? iValcVal1 /????? ?a???16
??Unicode?97???32??int? , ?1 ??,iVal???98/ System
.out.println(iVal) lVal(long) fVal
//??fVal ??????int?,????????LVal???5 System.out.pr
intln(lVal) dVal (fVal bVal) (iVal / cVal)
- (dVal sVal) System.out.println(dVal)
bVal (byte) (bVal 2) System.out.println(bVal)

80
bVal bVal 2 //Compiling Error. Cannot
assign an int to a byte bVal (byte) (bVal
2) // //????
81
3.3.9 ????
  • ?????????????
  • ??????????????????????????????????????????????

82
????
  • 1??????
  • ????????????.
  • 2????
  • ?????

?? a b a b c f
//???????
83
????
  • 3?????(???)
  • ?????? ???,????????
  • ??,
  • int x,y,z
  • x 5y 6z x y

84
3.4 ?????????
  • ????????????
  • ?????
  • ?????
  • ????.
  • ???????????????????

85
?????????
false
true
false
true
?3-4 ??????????????
86
?????????
  • ?????????????Java??
  • ????if-else, switch
  • ????while, do-while, for
  • ????????????
  • break, continue, return

87
3.4.1????
  • ?????????????,???????????????????.
  • if??
  • switch??

88
1.if ?? ????
  • (1) if (???) ??1
  • ?if ( x lt 0) x -x
  • (2) if (???) ??1 else ??2
  • ?if (xgty) zx
  • else zy

? if (xgt0) y1 else if (x0) y0 else if
(xlt0 xgt-10) y -1
89
if??????
?3-5 ?else???if??????
?else???if??????
90
  • (3) if (???1) ??1
  • else if (???2) ??2
  • else if (???3) ??3
  • else ?? n

91
if (xgt5) if (ygt5)
System.out.println(x and y are gt5) else
System.out.println(x gt5 and ylt5)
if (xgt5) if (ygt5)
System.out.println(x and y are gt5) else
System.out.println(x is lt5)
  • Nested ifelse statements
  • ifelse statements can be put inside other
    ifelse statements
  • Dangling-else problem (else?if???)
  • elses are always associated with the immediately
    preceding if unless otherwise specified by braces

if (xgt5) if (ygt5)
System.out.println(x and y are gt5) else
System.out.println(x is lt5)
if (xgt5) if (ygt5) System.out.println(
x and y are gt5) else
System.out.println(x gt5 and ylt5)
92
If (gradegt60) System.out.println( You
have passed) else System.out.println( You
have failed) System.out.println( You must
take this course again)
  • Blocks
  • Braces associate statements into blocks
  • Blocks can replace individual statements as an if
    body

If (gradegt60) System.out.println( You
have passed) else System.out.println( You
have failed) System.out.println( You must
take this course again)
93
?????????
  • ?3-7 ??????,??????
  • ?????????????4??????100??,???400???
  • ?????
  • ????????year?,
  • ????????,
  • ?????????

94
import javax.swing.JOptionPane class LeapYear
public static void main(String args)
int year boolean isLeapYear String sYear
JOptionPane.showInputDialog ( "Enter the
year") yearInteger.parseInt( sYear )
isLeapYear (year 4 0
year 100 ! 0)(year 400 0) if
(isLeapYear) sYearyear " is a leap year"
else sYear year " is not a leap year"
JOptionPane.showMessageDialog( null, sYear,??,
JOptionPane.PLAIN_MESSAGE )
95
?????????
  • ?3-8 ?????????,?????????.

96
import javax.swing.JOptionPane public class
FindMax public static void main( String
args ) String input1,input2,input3
int a,b,c,max input1
JOptionPane.showInputDialog( "?????? " )
input2 JOptionPane.showInputDialog(
"?????? ") input3 JOptionPane.showInputD
ialog( "?????? ") a Integer.parseInt(
input1 ) b Integer.parseInt( input2 )
c Integer.parseInt( input3 )
97
if(agtb) if (agtc) max a
else max c else
if (cltb) max b
else max c
JOptionPane.showMessageDialog( null, a", "b",
"c ?????? max, ?????,
JOptionPane.PLAIN_MESSAGE )
agtb agtc
bltaltc
altb cltb
altb bltc
98
2.switch ??
  • ????
  • switch (???)
  • case ????? 1??1
  • case ????? 2??2
  • ?
  • case ????? n??n
  • default ??n1
  • ????
  • ?case?????????????,???????????,??case???????break?
    ??

99
switch????? break
  • char myGrade myGrade 'A'
  • switch (MyGrade)
  • case 'A' MyScore 5
  • case 'B' MyScore 4
  • case 'C' MyScore 3
  • default MyScore 0

switch (MyGrade) case 'A' MyScore
5 break case 'B'
MyScore 4 break case
'C' MyScore 3 break
default MyScore 0
100
switch????? break
  • switch (MyGrade)
  • case 'A'
  • case 'B'
  • case 'C' MyScore 1 //??
  • break
  • default MyScore 0 //???

?????case?????????????????????????
101
????????
  • ?3-9 ????0100???,??????????????(A,B,C,D,E)????
  • ??Java Application??????????????.

102
class SwitchDemo public static void
main(String args) int scoreInteger.parseI
nt(args0) char grade switch
(score/10) // ?????????????? case 10
case 9 grade'A'
//??10?9??????? break case 8
grade'B' break case 7
grade'C' break case 6
grade'D' break default
grade'F' System.out.println("grade
is"grade)
103
(No Transcript)
104
?3-9 ??break?switch????????
105
3.4.2 ????
  • ???????????,?????????????,??????????????.
  • Java?????????
  • while???
  • do-while??
  • for??

???12100????? sum0
sumsumi ?? (i?1 ?100 )
106
1.while ??
  • while ????
  • while (?????) ??
  • ????

107
while ?????
  • ?3-10 ?while???12100???
  • ??
  • sum0
  • i1
  • sumsumi (?ilt100 )
  • ii1

????? sum?0
???i ?????1
while (ilt100) sumsumi ii1
108
public class WhileTry public static void
main(String args) int i,sum sum0
//??????0 i1
//???i?????1 while (ilt100)
sumi i System.out.println("sum
"sum)
sum5050
???1357.99?
???2468.100?
109
2.do-while ??
  • ????
  • do ??
  • while (???)
  • ????
  • ?while ?????

110
public class DoWhileTry public static void
main(String args) int i,sum sum0
//??????0 i1 //???i?????1 do
sumi i while (ilt100)
System.out.println("sum"sum)
?12100??
111
??????,????????
String output"" do rightDigitn10
outputoutputrightDigit nn/10
while (n!0)
  • ?3-12 ??????,???????????
  • ??12345 54321
  • ?? ??????10??????,??????0????????n?0,??0?

?(n!0)
???do-while???
112
import javax.swing.JOptionPane class
ReverseDigits public static void main (String
args) int n,rightDigit String str
JOptionPane.showInputDialog( "Input a
integer number") String output"" n
Integer.parseInt(str) do
rightDigitn10 outputoutputrightDigit
nn/10 while (n!0)
JOptionPane.showMessageDialog( null, "The reverse
digits is "output,"??", JOptionPane.PLAIN_MESSA
GE )
113
?3-12????????
114
3.for ??
  • ????
  • for (???1???2???3) ??

??for(i1ilt100i) sumi
  • ????

115
?3-13 ?for???12100???
public class ForTry public static void
main(String args) int i
//???????? int sum0 //??????0
for(i1ilt100i) sumi
System.out.println("sum"sum)
for(i1ilt100ii2)
for(i2ilt100ii2)
???1357.99?
???2468.100?
??????? i100 to 2
116
?3-14 ??????,?????????
  • ?????1n???????,?????n???n???,?nk0 (k?1???n)

class Factors public static void main(String
args) String inputargs0 int
nInteger.parseInt(input) for (int
k1kltnk) if (nk0)
System.out.print(k" ") System.out.println(""
)
117
??for???????
  • (1)for????????????(???????)

for ( ) ?? //??? while
(true) ?? for ( ilt100) ?? //???
while (ilt100) ??
(2)????1????3????????????
sum0 for(int i1ilt100i)
//?for??????????????? sumi
System.out.println(i) //!Errir
for(sum0, int i1ilt100i) sumi
118
sum0 i1
//?for?????????????? for ( ilt100 i)
sumsumi //?????1
i1
//?for?????????????? for (sum0 ilt100 i)
sumsumi //???1?????????
for (sum0, i1 ilt100)
//?????3 sumsumi i
//?????????????
for( i0, j10 iltj i, j--) //
???1????3????????
119
4.?????
  • ???????????????????,????????
  • ?????????????,????????
  • ??????(while??, do-while???for??)???????????

120
?3-15 ??????????
  • ??
  • ??????4???i1 to 4
  • ??????3??? i3 to 1
  • ???4-i ?
  • ???2i-1 ?
  • ??

for(i1ilt4i) //???4? for(j1jlt4-ij) S
ystem.out.print( ) for(j1jlt2i-1j) Sys
tem.out.print() System.out.println()
  • ???4-i ?
  • ???2i-1 ?
  • ??

121
public class Star public static void
main(String args) int i,j //
???????? for(i1ilt4i) //???4?
for(j1jlt4-ij) System.out.print(" ")
//??? for(j1jlt2i-1j) System.out.print
("") //??? System.out.println() // ??
// ???????? for(i3igt1i--) //???3?
for(j1jlt4-ij) System.out.print(" ")
//??? for(j1jlt2i-1j) System.out.print(
"") //??? System.out.println() // ??

122
6.????
  • ?????????
  • break??
  • continue??
  • return

123
(1) continue??
  • continue???????????
  • ??????????,??????????????????
  • continue???????
  • continue ??

124
continue??
  • ?????continue??,???????????????,?????????,????????
    ?????
  • ?for???,?????continue?????????3,?????????????????
  • ?while?do while???,?????continue????????????????

125
continue??
  • ?3-16 ???????continue????????

int count for (count1countlt10count)
if (count5) continue
System.out.print(count " ")
System.out.println("count"count)
???????????? 1 2 3 4 6 7 8 9 10
count11
126
continue??
  • ????continue????
  • continue ???
  • ??????????????????,???????????
  • ????continue??????????????????????

127
continue??
  • ?3-17??????continue????????.
  • ??1100??????

??i?????????? ??????i?????j(?j ?2? i-1???)
,?????i????.
  • OutterLoop
  • i ? 1 ?100 ?
  • j?2? i-1?
  • if (ij 0) continue
    OutterLoop
  • i?????

128
  • OutterLoop
  • for (int i1ilt100i)
  • for (int j2jltij)
  • if (ij0)
  • continue OutterLoop
  • System.out.println ( i ) //????????
  •  
  • OutterLoop
  • i ? 1 ?100 ?
  • j?2? i-1?
  • if (ij 0) continue
    OutterLoop
  • i?????

???????????? 1 2 3 5 7 11 13 17 19
.
129
(2)break??
  • break??????switch???????.
  • ????????????????????. ??switch????????,????????
    ??
  • break???????
  • break ??

130
break??
  • ?3-18 ???????break????????

int count for (count1countlt10count)
if (count5) break
System.out.print(count " ")
System.out.println("count"count)
???????????? 1 2 3 4 count5
131
break??
  • ?3-19??????break????????

stop for (int i1ilt10i) for (int
j1jlt5j) if (i5) break stop
System.out.print( "" ) System.out.printl
n ( "" )

132
3.5 ??
  • ??????Java????????????(??????????????)????????????
    ????
  • ??????,????????????????????????,???????????????,?
    ??????????,??????????

133
??
  • ?????
  • ?????
  • ???????
  • ?????
  • ????????

134
3.5.1?????
  • ???????
  • ????? ???(?????) //???
  • ???? //???

(1)??????????????????????.???????,???void????????
??void??,????????????????return ???
(2) return ????? return
??? return?????????????????,???????????????????
????(void??),???return???
135
?????
  • (3)??????????
  • ??1 ??1,??2 ??2,?,??n ??n
  • ??????????????????????????????????????????????,???
    ??????
  • (4)??????? ????????????,????????????????
  • ??????????????,??????
  • ??????????????????
  • ????,????????,?????????

int x,y,z,m x10 y89 z-78
int max(int a,int b) if (agtb) return a
else return b
mmax(max(x,y),z)
136
3.5.2?????
  • ???????
  • (1) ???(???)
  • (2) ???????????
  • ??????.???(???)
  • (3)??????????
  • ??.???(???)
  • ?????????????????????????????????????????????
  • ???????????(void), ????????????????????

Fector(5) String sab //snew
String(ab) s.length() JOptionPane.showInputD
ialog( "Input a integer number")
137
??????????
int max(int a,int b) if (agtb) return a
else return b
void p()
138
??????????
  • ?3-21 ??10000?????????
  • ?????????????(??1?????????)?????
  • 6123,6123,?6???????

?????? boolean isPerfect(int x),????x?????
void displayPerfect(int x),????x?????????
?main()???????for??,??1?10000??????????????
139
public class PerfectNumber public static
void main(String args) for(int x
1xlt10000x) if(isPerfect(x)) displayPer
fect( x) static boolean isPerfect(int x)
//??x????? int y0 for(int
i1iltxi) if(xi0)
//i?x??? yi if(y x) return
true else return false
?????mian??????????static????
140
static void displayPerfect(int x)
//??x???????? System.out.print(x"
") for(int i1iltxi) if(xi0)
if (i!1) System.out.print("")
System.out.print(i)
System.out.println()
????????
141
3.5.3???????
  • ????????????
  • (1) ??????????????????
  • ????????????
  • ????????????????????????????
  • (2) ??????????????????(?????????)?,
  • ??????????????????,
  • ?????????????

142
????
  • ??,
  • void swap( int x,int y)
  • int holdx xy yhold //?????

????
int a1,b2 swap (a,b)
//????????,a????1,b???2
???????
143
?????
class Integer int n Integer(int n1)
nn1 public String toString()
return String.valueOf(n)
static void swap(Integer x,Integer y) int
hold x.n x.ny.n y.nhold
  • Integer aobjnew Integer(a)
  • Integer bobjnew Integer(b)
  • swap(aobj,bobj)

144
?????
  • method1(Date d)
  • d.setDate(2007,10,30)

?????
Date d1new Date() method1(d1) Date
d2new Date() method1(d2)
145
?????
  • void bubbleSort( int array )
  • // ?????,??array????

?????
  • int array110,234,67,12
  • bubbleSort(array1)
  • int array210,234,67,12
  • bubbleSort(array2)

146
3.5.4?????(Overloading)
  • ?????????????????.
  • ??????????????????
  • ????????????????????,???????????????????????????
  • ????????
  • ?????????
  • ??????????

147
?????
  • ?3-22 ?????????int???double??????

148
????int????square??
public class MethodOverload public static
void main(String args) System.out.println(
"??? 7 ???? " square( 7 ))
System.out.println( "????7.5 ???? " square(
7.5 )) // ????int????square??
public static int square( int intValue )
System.out.println("??int????square??")
return intValue intValue //
???double????square?? public static double
square( double doubleValue )
System.out.println("??double????square??")
return doubleValue doubleValue
???double????square??
149
3.5.5?????
  • ????
  • ?????????,????????
  • ????
  • ????????????

void fun1(..) . fun1() .
150
??
  • ???????????????????????????????????.
  • ?3-23 ???n!?
  • n! 1 (n1)
  • n(n-1)! (ngt1)

???n!????????
long Factorial(int n)
//???? if(n 1) return 1
//????? else return
nFactorial (n-1) //??????
151
??
long Factorial(int n) if(n 1) return
1 else return nFactorial
(n-1)
  • ????yFactorial(5)?????

152
3.5.6????????
  • (1)???????????????????
  • (2)???????????????????,?????????????(?????????????
    )
  • (3)?????break?continue???,?????????????????(??????
    ???)
  • (4)???for?????????????,?????for???????????????
  • (5)catch?????????????????????,???????Catch???????
  • (6)???????(????),?????????????????????????????????
    ??????????????????,????????????????

153
class C1 int g void
method1(int k) int i ikg
  void method2(int k) int g
int j0 gjk
this.gjg i10
??????g
????k
??????i
???k?????g??,??????????i
????k
??????g
??????j
?????g??
??????j?g??,??????g
??!i?????
??this.g??????g
154
3.6 ??
  • ????????????????????????
  • ????
  • (1) ????????????????
  • (2) ???????????????????????????????????
  • (3) ???????????????????????????(???????????)?

??,????5????????a,??? int a new int 5
, ?????????????
????
???
155
??
  • ??????????
  • Java???????,????????,????????new???????
  • ??
  • ????
  • ????

156
3.6.1 ????
  • 1.??????
  • ?????? ???
  • ?
  • ?????? ???

??int intArray
2. ?????? ??? new ?????? ???????
?? intArray new int 10
?????intArray??10?????????????,?????????0 ?
?intArray0, intArray1, intArray9????0
157
????
int intArray intArray new int 10
????
  • int intArray new int 10

String stringArray new String5 // ???null

158
3.6.1 ????
  • String stringArray new String5 // ???null
  • ?new???????????????,?????????????????,???????????
    ??
  • ?????????????0,
  • ????????????????????(\u0000),
  • ???????????false,
  • ???????????,??????null?
  • ??????????,?????????,??????????????

159
3.6.1 ????
  • 3.??????
  • (1)??????,?????.?????????????????????

int a 1,2,3,4,5 //???????5??????a, //a01
,a12,a23,a34,a45?
(2)?????????,?????????????
int a new int5 a01,a12,a23,a34,a
45?
160
????
  • ??
  • String stringArrar //????String???????
    stringArray new String3
  • //???stringArray??3?????
  • stringArray0new String(how)
  • //????????
  • stringArray1new String(are)
  • stringArray2new String(you)

161
3.6.1 ????
  • 4.???????
  • ???????????
  • ?????
  • ???????????????????????????0?????????1?
  • ??, int intArraynew int10
  • ?intArray0, intArray1,., intArray9

intArraylength-1
  • ???????????length,??????????????.

intArray.length ???10
162
????
  • Java????????????????????
  • ??????????,??Java????????????,???????ArrayIndexOu
    tOfBoundsException???,
  • ???????????????
  • ?????????????????????length????????????

int intArraynew int10 intArray10100
//error intArray intArray.length 1100
163
3.6.1 ????
  • ?3-23 ????????

164
import javax.swing.JOptionPane public class
SumArray public static void main( String
args ) int array 1, 2, 3, 4,
5, 6, 7, 8, 9, 10 int total 0
for ( int counter 0 counter lt array.length
counter ) total array counter
// add each element's value to total
System.out.println("Total of array elements "
total) System.exit( 0 )
for ( int counter array.length-1 counter gt0
counter-- )
?????? 55
?????array?????????
165
3.6.2 ????
  • ?3-24 ??????????????

???????????????????,???????????. ???????????
bubbleSort(int array) ??????????????,???(????)??
???
????
166
???????
???????? pass1, element from 0 to 3 pass2,
element from 0 to 2 pass3, element from 0 to
1 pass4, element from 0 to 0
  • 1 15 9 -8 20

15 9
?1?
1 9 15 -8 20
1 9 -8 15 20
9 -8
?2?
1 -8 9 15 20
1 -8
?3?
-8 1 9 15 20
?4?
-8 1 9 15 20
pass from 1 to 4, element from 0 to (4-
pass) pass from 1 to length-1, element from 0 to
(length- pass-1)
167
  • array2??,????array2.length,???????
  • array20, array21, ,array2length-2,
    array2length-1
  •  
  • ?1????????????,element?0? length-2
  • array20, array21, ,array2length-3,
    array2length-2 array2length-1
  • ???????,????? array2length-1
  • ?2?element?0? length-3
  • array20, array21, ,array2length-3,
    array2length-2, array2length-1
  • ???????,????? array2length-2
  • ?3?element?0? length-4
  • ?length-1?(????) element?0?0,
  • array20, array21

element
element
for (pass 1 pass lt array2.length-1 pass )
for (element 0 element lt array2.length
pass -1element ) if (array2elementgt
array2element) ???
168
  • public void bubbleSort( int array2 )
    //??????????
  • // ??????????
  • for ( int pass 1 pass lt array2.length-1
    pass ) //??????????
  • for ( int element 0element
    ltarray2.length-pass- 1element)
  • if ( array2 element gt array2 element 1
    ) //??,??
  • int hold array2 element
  • array2 element array2element 1
  • array2 element 1 hold
  • // end loop to control passes

Flagtrue
Flagfalse
boolean Flagtrue
Flagtrue
????Array2.length-1?
?????????
169
  • ????????

170
3.6.2 ????
  • ??????????????????
  • ??,a23
  • ?Java???,?????????????
  • ?????????????????
  • ??????????????????
  • int anew int23

a0 a1
171
3.6.2 ????
  • ??????????
  • ?? ???
  • ??,
  • int a
  • a new int23
  • ???
  • int a new int23

172
3.6.2 ????
?????????????. b.length??2 b0.length??3 b1.l
ength??5
  • int b new int2
  • // ????2???,?????????????
  • b0 new int3
  • // ??????????????3?????
  • b1 new int5
  • // ??????????????5?????

 
173
????
  • 2. ????????
  • ?????????,???????????
  • int b 1,2,2,3,3,4
  • //????3?2??????,????????????

b00? b01?
b22? b30? b.length?
b0.length? b1.length?
  • int c 2,3,4,5,6
  • //??2??????c,?0?????????2,?1?????????3?

c00? c01? c10?
c11? c12? c.length?
c0.length? c1.length?
174
?3-25 ????????
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
  • ????????????
  • int yanghui
  • 1,1,1,1,2,1,1,3,3,1,1,4,6,4,1

for(i0iltyanghui.lengthi)
for(j0jltyanghuii.lengthj)
System.out.print("\t"yanghuiij)
System.out.println()
??????????
??????????
??
175
class YangHui public static void main(String
args) int i,j int yanghui
1,1,1,1,2,1,1,3,3,1,1,4,6,4,1
for(i0iltyanghui.lengthi)
//?????????? for(j0jltyanghuii.lengthj)
//????????1? System.out.print("\t"yangh
uiij) System.out.println()
176
??
  • Java?????????????????????
  • Java?????????????????????????????
  • Java???????????????????.
  • ????????????????????????????????????????????????
    ????????????????,??????????????,???????????????

177
  • ????????????????????????????
  • ???????????????????????????????????????
  • ??????????????????,????????,??????????????

178
?????
  • ??
  • ?
  • 1
  • 2
  • 4
  • 9
  • ??

179
00xf0gtgt27
  • class Example public static void main(String
    args)
  • System.out.print(-31gtgtgt5gtgt5gtgtgt5gtgt5gtgtgt5gtgt5)
  • //???3.Why?
  • 0000,0000, 0000,0000, 0000,0000,
    0001,1111
  • -31 1111,1111, 1111,1111, 1111,1111,
    1110,0001
  • -31gtgtgt5 0000,0111, 1111,1111, 1111,1111,
    1111,1111
  • 5?0, 27?1
  • ?? 30?0, 2?1

180
ASCII?
  • 7 ?(007F)? 32 127 ?????32 ???, 32
    ???????(???)??8???????

181
(No Transcript)
182
Unicode
  • Unicode ????,????? 65,536 ???,
  • ? Unicode ?, ????????????? code point
  • U ?? Unicode, ??? 16 ???,
  • ????? "Hello", ? Unicode ????? 5 ? code points
    U0048 U0065 U006C U006C U006F
  • ??? Unicode Encoding, ??????????????? "Hello"
    ???00 48 00 65 00 6C 00 6C 00 6F

183
UTF-8
  • UTF-8 ????? Unicode code points ?????????? U
    ??,?????? 8 bit. ? UTF-8 ?,???? 0127 ? code
    point ???????
  • "Hello" ?????,Unicode code point ? U0048 U0065
    U006C U006C U006F, ????? 48 65 6C 6C 6F??
    ASCII, ANSI, ????????????? OEM ??????????????????
    ???? ASCII ????

184
  • UCS-2(??? 2 ???) ?? UTF-16(??? 16 ??)
  • UTF-7,?????????? UTF-8 ??,???????? 0.
  • UCS-4, ????? code point ? 4 ??????????????????????
    ?
Write a Comment
User Comments (0)
About PowerShow.com