第4章数组与字符串.ppt
《第4章数组与字符串.ppt》由会员分享,可在线阅读,更多相关《第4章数组与字符串.ppt(25页珍藏版)》请在三一文库上搜索。
1、Java 语言程序设计(一)串讲,主讲人:周立民 更多优质自考资料尽在百度贴吧自考乐园俱乐部(http:/ 数组和字符串,本章重点 数组的应用 字符串常用方法 字符串与基本类型数据之间的转换,数组定义,数组是一个变量,存储相同数据类型的一组数据,int类型,元素,博物架名,标识符,古玩,物品编号,元素下标,物品类型,元素类型,数据,使用数组四步走: 1、声明数组 2、分配空间 3、赋值 4、处理数据,如何使用数组,int a;,a = new int5;,a 0 = 8;,a 0 = a0 * 10;,a,8,80,a0,数组的声明,int score1; /Java成绩 int score2
2、 ; /C#成绩 String name; /学生姓名,声明数组: 告诉计算机数据类型是什么,1,数据类型 数组名 ;,数据类型 数组名 ;,数组初始化,score = new int30; avgAge = new int6; name = new String30;,30,分配空间: 告诉计算机分配几个格子,数组属于引用数据类型,2,数据类型 数组名 = new 数据类型大小 ;,声明数组并分配空间,数组赋值,score0 = 89; score1 = 79; score2 = 76; ,赋值:向分配的格子里放数据,30,score0,score1,score2,89,79,76,太麻烦!
3、能不能一起赋值?,3,数组赋值,方法1: 边声明边赋值 方法2:动态地从键盘录入信息并赋值,解决,int score = 89, 79, 76;,Scanner input = new Scanner(System.in); for(int i = 0; i 30; i +) scorei = input.nextInt(); ,int score = new int 89, 79, 76;,使用数组求平均值,int score = 60, 80, 90, 70, 85; double avg; avg = (score0 + score1 + score2 + score3 + score4
4、)/5;,int score = 60, 80, 90, 70, 85; int sum = 0; double avg; for(int index = 0; index score.length; index+) sum = sum + scoreindex; avg = sum / score.length;,成绩单,访问数组成员:使用“标识符下标”,访问成员,数组的length属性,对数据进行处理:计算5位学生的平均分,4,public class HelloAccp2 public static void main(String args) int score = new int ;
5、 score0 = 89; score1 = 63; System.out.println(score0); ,常见错误,编译出错,没有写明数组的大小,public class HelloAccp3 public static void main(String args) int score = new int2; score0 = 89; score1 = 63; score2 = 45; System.out.println(score2); ,常见错误,编译出错,数组越界,常见错误,public static void main(String args) int score = new
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数组 字符串
链接地址:https://www.31doc.com/p-3430016.html