chapter7My--Java教案_第1页
chapter7My--Java教案_第2页
chapter7My--Java教案_第3页
chapter7My--Java教案_第4页
chapter7My--Java教案_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第七章 几个基本的Java类7.1 Math类1、常用的数学方法成员变量staticdoubleE The double value that is closer than any other to e, the base of the natural logarithms.staticdoublePI The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.成员方法staticdoubleabs(doublea) Retur

2、ns the absolute value of a double value.staticfloatabs(floata) Returns the absolute value of a float value.staticintabs(inta) Returns the absolute value of an int value.staticlongabs(longa) Returns the absolute value of a long value.staticdoubleceil(doublea) Returns the smallest (closest to negative

3、 infinity) double value that is not less than the argument and is equal to a mathematical integer.staticdoublecos(doublea) Returns the trigonometric cosine of an angle.staticdoubleexp(doublea) Returns the exponential number e (i.e., 2.718.) raised to the power of a double value.staticdoublefloor(dou

4、blea) Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.staticdoublelog(doublea) Returns the natural logarithm (base e) of a double value.staticdoublemax(doublea, doubleb) Returns the greater of two double val

5、ues.staticfloatmax(floata, floatb) Returns the greater of two float values.staticintmax(inta, intb) Returns the greater of two int values.staticlongmax(longa, longb) Returns the greater of two long values.staticdoublemin(doublea, doubleb) Returns the smaller of two double values.staticfloatmin(float

6、a, floatb) Returns the smaller of two float values.staticintmin(inta, intb) Returns the smaller of two int values.staticlongmin(longa, longb) Returns the smaller of two long values.staticdoublepow(doublea, doubleb) Returns of value of the first argument raised to the power of the second argument.sta

7、ticdoublerandom() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.staticlonground(doublea) Returns the closest long to the argument.staticintround(floata) Returns the closest int to the argument.staticdoublesin(doublea) Returns the trigonometric sine of an

8、 angle.staticdoublesqrt(doublea) Returns the correctly rounded positive square root of a double value.staticdoubletan(doublea) Returns the trigonometric tangent of an angle.staticdoubleatan(doublea) Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.staticdoubletoRadians(doublea

9、ngdeg) Converts an angle measured in degrees to the equivalent angle measured in radians.2、程序演示运行结果为:3、产生9个100以内的随机数 public class MathDemo2public static void main(String args)int r;for (int i=1;i10;i+)r = (int)( 100*Math.random() );System.out.println( random()= + r );7.2 数组1、数组基础 在Java里,数组就是对象,数组类型是

10、引用类型,数组变量实际上是对数组的引用。 Java数组是动态分配的,并在分配过程中记录数组的长度。 1)建立数组第一步:声明 数组类型 数组名;如:int a;第二步:分配数组空间 数组名new数组类型 n 其中n是正整数,表示数组大小 如:a = new int5;第三步:给数组元素赋值 a0 = 0; a1 = 1; a2 = 1; a3 = 1; a4 = 1;或者三步合而为一,在定义数组时直接初始化他的值:如:int a= 0 , 1 , 2 , 3 , 4 ;2)例程 public class ArrayForms1public static void main(String arg

11、s)/int a=3,7,9;int a=new int3;for(int i = 0; i 3; i+)System.out.print(ai+ );System.out.println();a0=3;a1=7;a2=9;for(int i = 0; i 3; i+)System.out.print(ai+ );System.out.println();boolean b=new boolean3;for(int i = 0; i 3; i+)System.out.print(bi+ );b0=true;b2=true;System.out.println();for(int i = 0;

12、i 3; i+)System.out.print(bi+ );运行结果:0 0 03 7 9false false falsetrue true true从中我们可以看到: int a=3,7,9; 等价于 int a=new int3; a0=3; a1=7; a2=9;2、数组长度 数组是对象,Java给了他一个成员变量length ,表示数组的长度。 int a=3,7,9; int len = a.length; / len=3 举例: public class Array2static void m(int a)for(int i = 0; i a.length; i+)System

13、.out.print(ai+ );System.out.println();public static void main(String args)int a1=3,7,9;int a2=1,3,6,0;m(a1);m(a2);3、对象数组 数组元素是对象,这样的数组称为对象数组。 Mankindpeople = new Mankind() , new Mankind() , new Mankind() ; 等价于 Mankindpeople = new Mankind3; people0 = new Mankind(); people1 = new Mankind();people2 = n

14、ew Mankind();程序演示:运行结果:0 0 01 2 34、多维数组1)建立一个二维数组 int a = 1,2,3,4; 二维数组a是由两个一维数组a0、a1构成的。 int b = 1,2,3,4,5,6,7,8; 二维数组中的一维数组长度可以不同。 注意: int c = 1,2,3,4,5,6,7,8 ; 非法2)二维数组的长度 int a = 1,2,3,4;int b = 1,2,3,4,5,6,7,8; a.length的值为2 b.length的值为4 a0.length的值为2 b2.length的值为3 程序演示: public class Array5publi

15、c static void main(String args) int a = 1,2,3,4,5,6;System.out.println(a.length:+a.length);System.out.println(a0.length:+a0.length);for(int i = 0; i a.length; i+)for(int j = 0; j ai.length; j+)System.out.println(int array+i+j+=+ aij);参见教材P187页程序Array6.java5、与数组有关的运行错误1) public class Array7 public st

16、atic void main(String args) int t=1,2,3,4,5,6;System.out.print(t12);2) public class Array8 public static void main(String args) /inta=new int0;/compile and run ok/intb=new int3.1;/compile errorintc=new int-3;/compile ok but run error7.3 String类 String类表示字符串,而字符串是双引号中的内容。1、字符串的声明和赋初值 String s = “It i

17、s a string”; 等价于 String s = new String(); s = “It is a string”;2、String类中的方法 成员方法charcharAt(intindex) Returns the character at the specified compareTo(StringanotherString) Compares two strings compareToIgnoreCase(Stringstr) Compares two strings lexicographically, ignori

18、ng case considerations.Stringconcat(Stringstr) Concatenates the specified string to the end of this string.booleanequals(ObjectanObject) Compares this string to the specified object.booleanequalsIgnoreCase(StringanotherString) Compares this String to another String, ignoring case i

19、ndexOf(Stringstr) Returns the index within this string of the first occurrence of the specified indexOf(Stringstr, intfromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified length() Returns the length of thi

20、s string.Stringreplace(charoldChar, charnewChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.booleanstartsWith(Stringprefix) Tests if this string starts with the specified prefix.booleanstartsWith(Stringprefix, inttoffset) Tests if this string

21、 starts with the specified prefix beginning a specified index.Stringsubstring(intbeginIndex) Returns a new string that is a substring of this string.Stringsubstring(intbeginIndex, intendIndex) Returns a new string that is a substring of this string.chartoCharArray() Converts this string to a new cha

22、racter array.StringtoLowerCase() Converts all of the characters in this String to lower case using the rules of the default locale, which is returned by Locale.getDefault.StringtoString() This object (which is already a string!) is itself returned.StringtoUpperCase() Converts all of the characters in this String to upper case using the rules of the default locale, which is returned by Locale.getDefault.Stringtrim() Removes white space from both ends of this string.staticSt

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论