第3讲Java语法基础2.ppt

上传人:本田雅阁 文档编号:2577545 上传时间:2019-04-11 格式:PPT 页数:68 大小:345.51KB
返回 下载 相关 举报
第3讲Java语法基础2.ppt_第1页
第1页 / 共68页
第3讲Java语法基础2.ppt_第2页
第2页 / 共68页
第3讲Java语法基础2.ppt_第3页
第3页 / 共68页
亲,该文档总共68页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第3讲Java语法基础2.ppt》由会员分享,可在线阅读,更多相关《第3讲Java语法基础2.ppt(68页珍藏版)》请在三一文库上搜索。

1、1,第3讲 Java语法基础 (2),武汉大学国际软件学院,2,3.1 语句 (statement),表达式 + 分号“;”表达式语句 x = 25; y += a*b+c; a+b; 只有分号空语句 i = 5; ; ; 符合语法规则(程序设计的初始阶段),3,3.1.1 条件选择语句,if 语句 if语句是一个条件表达式,若条件表达式为真,则执行下面的代码块,否则跳过该代码块 单行代码 if (布尔表达式) 语句; 多行代码 if (布尔表达式) ; 语句; ,4,3.1.1 条件选择语句 (续),示例 import java.io.IOException; class Test publ

2、ic static void main(String args) throws IOException System.out.println(“你喜欢Java吗(Y/N) “); char like = (char)System.in.read(); if (like = Y | like = y) System.out.println(“Good”); ,5,3.1.1 条件选择语句 (续),if-else 语句 根据判定条件的真假执行不同的操作 语法 if (布尔表达式) 语句块1; else 语句块2; ,6,3.1.1 条件选择语句 (续),示例 import java.io.IOEx

3、ception; class Test public static void main(String args) throws IOException System.out.println(“请输入你的成绩: “); char a = (char)System.in.read(); char b = (char)System.in.read(); int score = (a-0)*10 + b-0; if (score = 60) System.out.println(“你及格了!”); else System.out.println(“你没及格了!”); ,C:java Test 请输入你

4、的成绩: 65 你及格了! C:,7,3.1.1 条件选择语句 (续),if 语句的嵌套 if 语句中的语句块又出现了if 语句 若没有配对符,则else与最近的if语句配对 例 int a=1, b=2, c=3; if (ac) if (cb) System.out.print( c ); else System.out.print(a); 一定要明确地写上配对符,修改配对关系: if (ac) if (cb) System.out.print( c ); else System.out.print(a); ,默认Java虚拟机: if (ac) if (cb) System.out.pr

5、int( c ); else System.out.print(a); ,8,3.1.1 条件选择语句 (续),条件运算符 三元运算符(ternary operator): “? : ” 表达式1 ? 表达式2 : 表达式3 表达式1的结果为布尔型,表达式2和表达式3的类型相同 表达式1true表达式2 表达式1false表达式3 Shortcut if-else statement,if (表达式1) 表达式2 else 表达式3,9,3.1.1 条件选择语句 (续),示例 import java.io.IOException; class Test public static void m

6、ain(String args) throws IOException System.out.println(“请输入三个09之间的数“); byte x = (byte)System.in.read(); byte y = (byte)System.in.read(); byte z = (byte)System.in.read(); x -= 48; y -= 48; z -= 48; byte n = xy? x : y; byte m = n z? n : z; System.out.println(“max= “+m); ,char 0 (48) char 1 (49) ,x=x-4

7、8; y=y-48; x=z-48;,C:java Test 请输入三个09之间的数 370 max= 7 C:,10,3.1.1 条件选择语句 (续),示例 import java.io.IOException; class Test public static void main(String args) throws IOException System.out.println(“中国足球能否进入世界杯?“); System.out.println(“是(y) 否(n) 不一定(p)“); char c = (char) System.in.read(); if (c = y) Syst

8、em.out.println(“Cool”); else if (c = n) System.out.println(“Bad”); else if (c = p) System.out.println(“Sorry”); else System.out.println(“Input Error”); ,逐条if语句进行判断 条件匹配,进入语句体 否则对if语句继续匹配,11,3.1.2 switch/开关语句,根据表达式的结果执行多个操作中的一个 语法 switch (表达式) case 值1: 语句序列; break; case 值2: 语句序列; break; default: 默认语句

9、; 与任一case值不匹配,则进入default语句,12,3.1.2 switch/开关语句 (续),语法 switch (表达式) case 值1: 语句序列; break; case 值2: 语句序列; break; default: 默认语句; ,几点注意 switch语句表达式的结果必须是byte, char, short, int 类型 表达式的结果依次与每个case子句比较 break语句用于跳出switch语句 default子句是可选的,13,3.1.2 switch/开关语句 (续),示例 1 import java.io.IOException; class Test p

10、ublic static void main(String args) throws IOException System.out.println(“中国足球能否进入世界杯?“); System.out.println(“是(y) 否(n) 不一定(p)“); char c = (char) System.in.read(); switch (c ) case y: System.out.println(“Cool”); break; case n: System.out.println(“Bad”); break; case p: System.out.println(“Sorry”); b

11、reak; default: System.out.println(“Input Error”); break; ,14,示例 2,public class Test public static void main(String args) int month = 2, year = 2000; int numDays = 0; switch (month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6:,case 9: case 11: numDays

12、 = 30; break; case 2: if (year % 4 = 0) ,3.1.2 switch/开关语句 (续),15,3.1.3 循环控制语句,反复执行同一代码块直到满足结束条件 组成 循环的初始状态 循环体 迭代因子(计数器的递增或递减) 控制表达式 3种循环语句 while循环 do-while循环 for循环,16,3.1.3 循环控制语句 (续),while循环 语法 while (布尔表达式) 循环体; ,17,3.1.3 循环控制语句 (续),示例 class Test public static void main(String args) int i, sum;

13、sum = 0; i = 0; while (i = 100) sum += i; i+; System.out.println(“Sum= ” + sum); ,循环体,控制表达式,18,3.1.3 循环控制语句 (续),do-while循环 语法 do 循环体; while(布尔表达式); 先执行循环体 后判断布尔表达式 循环体至少执行一次,19,3.1.3 循环控制语句 (续),示例 import java.io.IOException; class Test public static void main(String args) throws IOException char c;

14、StringBuffer buffer = new StringBuffer(); System.out.println(“输入一句子以.表示结束”); do c = (char) System.in.read(); buffer.append(c); while (c != .); System.out.println(“Output = ” + buffer.toString(); ,C:java Test 输入一句子以.表示结束 fdsfs. Output = fdsfs. C:java Test 输入一句子以.表示结束 fdsf中国. Output = fdsf?. C:,20,3.1

15、.3 循环控制语句 (续),for循环: 最有效、最灵活的循环结构 语法 for (初始化部分;条件判断部分;迭代因子) 循环体; 初始化部分 设置循环变量的初值 条件判断部分 任意布尔表达式 迭代因子 控制循环变量的增减,21,3.1.3 循环控制语句 (续),for语句求07之间任意数的阶乘 import java.io.IOException; class Test public static void main(String args) throws IOException int i, n, sum=1; System.out.println(“Please input(07):”)

16、; n = System.in.read(); n -= 48; for (i = 1; i = n; i+) sum *= i; System.out.println(n + “!= ” + sum); ,D:java Test Please input(07): 5 5!=120 1.能否算09 2.能否算12, 134?,22,3.1.3 循环控制语句 (续),for循环的几点注意 初始化部分和迭代因子可以包含多个语句,以“,”分开 for (int i=0, j=10; ij; i+, j-) 初始化部分、条件判断部分和迭代因子可以为空语句,但以“;”分开,表示无限循环 for ( ;

17、 ; ) / infinite loop . ,23,3.1.3 循环控制语句 (续),如果一个人出生于1970年,那么他这一辈子能有几个闰年(以70岁为寿命长度) int length = 70; int firstYear = 1970; int year; for (int i =0; i length; i+) year = firstYear + i; if(year%4=0 ,1972 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020 2024 2028 2032 2036,24,3.1.3 循环控制语句 (续)

18、,循环的嵌套 一个循环体内包含另一个完整的循环结构 嵌套的层次多,多重嵌套 while循环、do-while循环、for循环相互嵌套,25,3.1.3 循环控制语句 (续),求 1!+2!+3!+ +9! import java.io.IOException; class Test public static void main(String args) throws IOException int n, sum, total=0; System.out.println(“Please input(09):”); n = System.in.read(); n -= 48; for (int

19、j = 1; j = n; j+) sum = 1; for (int i = 1; i =j; i+) sum *= i; total +=sum; System.out.println(“各阶乘之和为: ” + total); ,求阶乘,阶乘之和,C:java Test Please input(09): 4 各阶乘之和为: 33,输入0的结果!,26,3.1.4 跳转/转向语句,将程序的执行跳转到其他部分的语句 break: 跳出(中止)循环 continue: 结束本次循环 return: 方法返回 throw: 抛出异常(Exception),27,3.1.4 跳转/转向语句 (续)

20、,break语句 break语句用以中断当前执行的循环语句(for、do-while、while)或switch语句 两种形式 不带标号的break语句 表达形式: break; 从本层循环中跳出 带标号的break语句 表达形式: 从整个程序块中跳出,标号: 程序块 (break 标号;) ,若只有一层循环,带标号和不带标号作用相同 若存在循环嵌套,两者作用不同,28,不带标号的break语句 class Test public static void main(String args) for (int j = 1; j 6; j+) if (j =3) break; System.out

21、.print(“j=“ + j); System.out.println(“ stop”); ,3.1.4 跳转/转向语句 (续),j=1 j=2 stop,29,带标号的break语句 class Test public static void main(String args) int j, k; Rep: for (j = 8; j 1; j-) for (k=1; k”); ,3.1.4 跳转/转向语句 (续),8 16 24 32 7 14 21 28 ,若只有一层循环,带标号和不带标号作用相同 若存在循环嵌套,两者作用不同,30,3.1.4 跳转/转向语句 (续),continue

22、语句 continue语句用以结束循环语句(for、do-while、while)的本次循环 两种形式 不带标号的continue语句 表达形式: continue; 结束本次循环,即跳过continue语句后的语句,返回至本层循环体的条件测试部分 带标号的continue语句 表达形式: 跳至标号所指语句块的条件测试部分继续执行 注意与break语句的比较,标号: 程序块 (continue 标号;) ,若只有一层循环,带标号和不带标号作用相同 若存在循环嵌套,两者作用不同,31,不带标号的continue语句 class Test public static void main(Strin

23、g args) for (int k = 6; k =0; k-=2) if (k = 4) continue; System.out.print(“k=“ + k + “t”); ,3.1.4 跳转/转向语句 (续),k=6 k=2 k=0,32,带标号的continue语句 class Test public static void main(String args) iLoop: for (int i=1; i =10) System.out.print(p +“ ”); else System.out.print(p + “ ”); System.out.println(); ,3.1

24、.4 跳转/转向语句 (续),2 4 5 6 12 15 8 16 20 10 20 50,若只有一层循环,带标号和不带标号作用相同 若存在循环嵌套,两者作用不同,33,import/包含语句 引入程序所需要的类 import java.io.*; import java.applet.Applet; package/打包语句 指明所定义的类属于哪个包 通常作为源程序的第一条语句 package test;,3.1.4 跳转/转向语句 (续),34,3.2 数组和字符串,数组是一组同类型的变量或对象的集合 数组的类型可以是基本类型,或类和接口 数组中每个元素的类型相同 引用数组元素通过数组名下

25、标 数组下标(数组的索引)从0开始 数组是一种特殊的对象(Object) 定义类型 (声明) 创建数组 (分配内存空间) : new 释放 (Java虚拟机完成) 一维数组、多维数组,35,3.2.1 一维数组,一维数组的元素只有一个下标变量 例: A1, c3 一维数组的声明 方法1: 类型 数组名; String args; int a; double amount; char c; 方法2: 类型 数组名; String args; int a; double amount; char c; 注意 类型是数组中元素的数据类型(基本和构造类型) 数组名是一个标识符 数组声明后不能被访问,因

26、未为数组元素分配内存空间,variable d might not have been initialized System.out.println(d0); 1 error,double d; System.out.println(d0);,36,3.2.1 一维数组 (续),数组的创建 用new来创建数组 为数组元素分配内存空间,并对数组元素进行初始化 格式: 数组名 = new 类型数组长度 例: a = new int3; 声明和创建的联用: int a = new int3; 默认赋初值 整型初值为0 int i = new int3; 实型初值为0.0 float f = new

27、float3; 布尔型初值为false boolean b = new boolean3; 字符型初值为u0000(不可见) char c = new char3;,37,3.2.1 一维数组 (续),class Test public static void main(String args) int i = new int3; float f = new float3; boolean b = new boolean3; char c = new char3; for (int j = 0; j i.length; j+) System.out.println(ij); for (int

28、j = 0; j f.length; j+) System.out.println(fj); for (int j = 0; j b.length; j+) System.out.println(bj); for (int j = 0; j c.length; j+) System.out.println(cj); ,C:java Test 0 0 0 0.0 0.0 0.0 false false false C:,38,3.2.1 一维数组 (续),一维数组的初始化 为数组元素指定初始值 方式一: 声明和创建数组后对数组初始化 class Test public static void m

29、ain(String args) int a = new int5; System.out.println(“t输出一维数组a: ”); for (int i = 0; i 5; i+) ai = i +1; System.out.println(“ta”+i+“=”+ai); ,a.length,39,3.2.1 一维数组 (续),一维数组的初始化 方式二: 在声明数组的同时对数组初始化 格式: 类型 数组名 = 元素1, 元素2 ; int a = 1, 2, 3, 4, 5; class Test public static void main(String args) int a =

30、1,2,3,4,5; System.out.println(“t输出一维数组a: ”); for (int i = 0; i 5; i+) System.out.println(“ta”+i+“=”+ai); ,40,3.2.1 一维数组 (续),数组的赋值 数组的整体赋值 用java.lang.System类的方法进行数组复制,41,3.2.1 一维数组 (续),数组整体赋值 class Test public static void main(String args) int a = 2, 4, 6, 8; int b; int c = 1, 3, 5, 7; b = a; c = a;

31、for (int j = 0; j a.length; j+) System.out.print(aj + “ “); System.out.println(); for (int j = 0; j b.length; j+) System.out.print(bj + “ “) ; System.out.println(); for (int j = 0; j c.length; j+) System.out.print(cj); ,C:java Test 2 4 6 8 2 4 6 8 2 4 6 8 C:,int a = 2, 4, 6, 8, 0; int b; int c = 1,

32、3, 5, 7;,int a = 2, 4, 6, 8; int b; int c = 1, 3, 5, 7, 9;,42,3.2.1 一维数组 (续),数组的复用(reuse),public class Test public static void main(String args) int array = 32, 87, 3, 589, 12, 1076, 2000 ; for (int i = 0; i array.length; i+) System.out.print(arrayi + “ “); array = new int4; for (int i = 0; i array.

33、length; i+) arrayi = i + 1; System.out.println(); for (int i = 0; i array.length; i+) System.out.print(arrayi + “ “); ,43,3.2.1 一维数组 (续),一维数组的数组复制 java.lang.System类的方法 public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length),44,3.2.1 一维数组 (续),class Test public stati

34、c void main(String args) int a = 2, 4, 6, 8; int b; int c = 1, 3, 5, 7, 9; b = a; System.arraycopy(a, 1, c, 0, 3); System.out.print(“数组a: ”); for (int i = 0; i a.length; i+) System.out.print(ai + “ ”); System.out.println(); System.out.print(“数组b: ”); for (int i = 0; i b.length; i+) System.out.print(

35、bi + “ ”); System.out.println();,一维数组的数组复制,System.out.print(“数组c: ”); for (int i = 0; i c.length; i+) System.out.print(ci + “ ”); System.out.println(); ,数组a: 2 4 6 8 数组b: 2 4 6 8 数组c: 4 6 8 7 9,45,3.2.1 一维数组 (续),数组的排序,public class Test public static void main(String args) int array = 32, 87, 3, 589,

36、 12, 1076, 2000, 8, 622, 127 ; for (int i = array.length; -i = 0; ) for (int j = 0; j arrayj+1) int temp = arrayj; arrayj = arrayj+1; arrayj+1 = temp; for (int i = 0; i array.length; i+) System.out.print(arrayi + “ “); ,46,3.2.1 一维数组 (续),小结 类型相同、数量确定的存储结构 用下标访问数组中任一个元素,数组名下标 声明、创建(new)、初始化/赋值,int d;

37、 System.out.println(d0); System.out.println(d.length);,variable d might not have been initialized System.out.println(d0); System.out.println(d.length); ,47,3.2.2 多维数组,数组的数组 Arrays of Arrays 例: 表格(行和列) 以二维数组为例,48,3.2.2 多维数组 (续),二维数组的声明 类型 数组名, 例 int a; 数组声明后不能被访问,因未为数组元素分配内存空间 二维数组的创建 方法一: 直接分配空间(new

38、) 例 int a = new int23; a00 a01 a02 a10 a11 a12 两个一维数组,每个数组包含3个元素,49,3.2.2 多维数组 (续),二维数组的创建 方法二: 从最高维开始,为每一维分配空间 例 int c = new int2; c0 = new int4; c1 = new int3; c00 c01 c02 c03 c10 c11 c12 注: 为数组分配空间需指定维数大小,至少最高维(最左边)大小 错误: int b = new int;,50,3.2.2 多维数组 (续),二维数组的初始化 对每个元素单独进行赋值 声明数组的同时初始化 对数组元素的引用

39、 数组名下标1 下标2 下标为非负的整型常数0,51,3.2.2 多维数组 (续),二维数组的初始化 每个元素单独进行赋值 class Test public static void main (String args) int a = new int33; a00=1;a11=1;a22=1; System.out.println(“数组a: ”); for (int i=0; i a.length; i+) for (int j=0; jai.length; j+) System.out.print(aij+“ ”); System.out.println(); ,最高维数组长度,1 0

40、0 0 1 0 0 0 1,52,3.2.2 多维数组 (续),二维数组的初始化 声明数组的同时初始化 例 int a = 1,2,3, 3,4,5; a00=1 a01=2 a02=3 a10=3 a11=4 a12=5 例 String cartoons = “Flint“,“Fred“,“Wim“,“Pebbles“,“Dino“, “Rub“,“Barn“,“Bet“, “Bam“, “Jet“,“Geo“,“Jane“,“Elroy“,“Judy“,“Rosie“, “Astro“, “Sco“,“Sco“, “Shag“, “Velma“, “Fred“, “Dap“ ;,int

41、i = cartoons.length,53,3.2.2 多维数组 (续),杨辉三角形 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 三角形腰上的数为1 其他位置的数为其上一行相邻两个数之和,54,3.2.2 多维数组 (续),杨辉三角形 用二维数组描述杨辉三角形 a11 a21 a22 a31 a32 a33 a41 a42 a43 a44 a51 a52 a53 a54 a55 第1列元素为1 对角线上的元素为1 其他元素aij=ai-1j-1+ai-1j,纵轴为i, 横轴为j 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 1

42、0 10 5 1,55,3.2.2 多维数组 (续),杨辉三角形,class Test public static void main(String args) int n=6, indent, i, j; int a = new intnn; a11=1; for (i=2; in; i+) ai1=1;aii=1; for(j=1; ji; j+) aij=ai-1j-1+ai-1j; ,纵轴为i, 横轴为j 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0,纵轴为i, 横轴为j 0 0 0 0

43、0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 2 1 0 0 0 1 3 3 1 0 0 1 4 6 4 1,56,3.2.2 多维数组 (续),杨辉三角形,indent=25; for (i=1; in; i+) for(int k=1; k=indent; k+) System.out.print(“ ”); for(j=1; j=i; j+) System.out.print(aij+“ ”); System.out.println(); indent=indent-2; ,纵轴为i, 横轴为j 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0

44、 1 2 1 0 0 0 1 3 3 1 0 0 1 4 6 4 1,1 1 1 1 2 1 1 3 3 1 1 4 6 4 1,57,3.2.2 多维数组 (续),例1 class Test public static void main(String args) int a; System.out.println(a.length); C:javac Test.java Test.java:4: variable a might not have been initialized System.out.println(a.length); 1 error,58,例2 class Test

45、public static void main(String args) int a = new int; System.out.println(a.length); C:javac Test.java Test.java:3: expected int a = new int; 1 error,3.2.2 多维数组 (续),59,3.2.2 多维数组 (续),例3 class Test public static void main(String args) int a = new int9; System.out.println(a.length); C:javac Test.java C:java Test 9,60,3.2.3 数组的界限,起点和终点 数组的长度: 数组名.length 起点: 数组名0 终点: 数组名length-1 int i = 4, 56, 78, 9, 34; i.length 5 i0 4 ilength-1=i434 ia 若a4 则?,61,3.2.4 命令行参数,JAVA应用程序的主方法(程序的入口) public static void main (String args) public stat

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1