Java面向對象程序的設計第9章用實用類_第1頁
Java面向對象程序的設計第9章用實用類_第2頁
Java面向對象程序的設計第9章用實用類_第3頁
Java面向對象程序的設計第9章用實用類_第4頁
Java面向對象程序的設計第9章用實用類_第5頁
已閱讀5頁,還剩64頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、1java面向對象程序設計面向對象程序設計 第第9章章常用實用類2主要內容主要內容string類類stringbuffer類類stringtokenizer類類date類類clendar類類math與與biginteger類類decimalformat類類pattern與與match類類難點難點pattern與與match類類導讀導讀39.1 stringstring類類 java.langjava.lang包中的包中的stringstring類來創建一個字符類來創建一個字符串變量,串變量,字符串變量是對象字符串變量是對象。 49.1.1 構造字符串對象構造字符串對象 1 1常量對象:常量對象

2、:字符串常量對象是用雙引號括起的字符串常量對象是用雙引號括起的字符序列,例如:字符序列,例如: 你好你好 、12.9712.97、boyboy等。等。 2 2字符串對象字符串對象 聲明:聲明:string s; string string類較常用構造方法類較常用構造方法: : string(s),string (char a),string(char a,int startindex,int count) string s1=“,mmn”;string s2=new string(“abd”);char c=a,m,h;string s3=new string(c); string s4=ne

3、w string(c,0,2);5 3引用字符串常量對象引用字符串常量對象 string s1 = how are you; string s2 = how are you;69.1.2 string string 類的常用方法類的常用方法 1 1public int length():public int length():獲取一個字符串的長度獲取一個字符串的長度 2 2public boolean equals(stringpublic boolean equals(string s):s):比較當前字符串對象的實體是否與參數比較當前字符串對象的實體是否與參數s s指定的字指定的字符串的實

4、體相同符串的實體相同 例題例題9-19-1public class example9_1 public static void main(string args) string s1,s2; s1=new string(we are students); s2=new string(we are students); system.out.println(s1.equals(s2); /輸出結果是:輸出結果是:true system.out.println(s1=s2); /輸出結果是:輸出結果是:false string s3,s4; s3=how are you; s4=how are y

5、ou; system.out.println(s3.equals(s4); /輸出結果是:輸出結果是:true system.out.println(s3=s4); /輸出結果是:輸出結果是:true 7 3public boolean startswith(string s) public boolean endswith(string s)方法方法: 判斷當前字符串對象的前綴(后綴)是否是參數判斷當前字符串對象的前綴(后綴)是否是參數s指定的字符串指定的字符串 4public boolean regionmatches(int firststart,string other,int ort

6、herstart,int length) : 從當前字符串參數從當前字符串參數firststart指定的位置開始處,指定的位置開始處,取長度為取長度為length的一個子串,并將這個子串和參數的一個子串,并將這個子串和參數other指定的一個子串進行比較指定的一個子串進行比較 。 其重載方法:其重載方法:public boolean regionmatches( boolean b, int firststart,string other,int ortherstart,int length) 參數參數b決定是否區分大小寫,為決定是否區分大小寫,為true忽略忽略 例題例題9-28 publi

7、c class example9_2 public static void main(string args) int number=0; string s=student;entropy;engage,english,client; for(int k=0;ks.length();k+) if(s.regionmatches(k,en,0,2) number+; system.out.println(“number=”+number); /輸出結果為 number=5 9 5public int compareto(string s): 按字典序與參數按字典序與參數s指定的字符串比較指定的字

8、符串比較大小大小 其相關方法:其相關方法: public int comparetoignorecase(string s) 例題例題9-3 10 /sortstring.java 起泡排序 public class sortstring public static void sort(string a) for(int i=0;ia.length-1;i+) for(int j=i+1;ja.length;j+) if(pareto(ai)int 15 可以使用可以使用string 類的類方法類的類方法 public static string valueof(int n) 等將形如等將形如

9、123、1232.98等數值轉化為等數值轉化為字字符串。符串。 可以使用可以使用long類中的類方法類中的類方法 public static string tobinarystring(long i) 等得到整數的等得到整數的各種進制各種進制的字符串表示的字符串表示。2) int - string16main方法中的參數方法中的參數public static void main(string args)主方法。其中參主方法。其中參數是字符串數組數是字符串數組args,這個數組的元素,這個數組的元素args0, args1 argsn的值都是字符串。的值都是字符串。args就是就是命令行命令行的

10、參數。的參數。在在java解釋器解釋用戶的字節碼文件時,可解釋器解釋用戶的字節碼文件時,可以包括需要傳給以包括需要傳給main方法的參數。一般形式為:方法的參數。一般形式為: java 類文件名類文件名 字符串字符串1 字符串字符串2 字符串字符串n 其中,類文件名和各字符串間用其中,類文件名和各字符串間用空格分隔。空格分隔。17補充例子:補充例子: public class c7_7 public static void main(string args) for(int i=0;iargs.length;i+) system.out.println(argsi); 18 運行時輸入運行時輸

11、入“java c7_7 hello world lets java!”命令,命令,則有如下的結果:則有如下的結果:helloworldlets java!19 public class example9_5 public static void main(string args) double aver=0,sum=0,item=0; boolean computable=true; for(string s:args) try item=double.parsedouble(s); sum=sum+item; catch(numberformatexception e) system.out

12、.println(您鍵入了非數字字符:+e); computable=false; 例例9.520 if(computable) aver=sum/args.length; for(string s:args) system.out.print(s+ ); system.out.println(的平均數:+aver); int number=8642; string binarystring=long.tobinarystring(number); system.out.println(number+的二進制表示:+binarystring); system.out.println(numbe

13、r+的十六進制表示:+long.tostring(number,16); 219.1.4 對象的字符串表示對象的字符串表示 所有類默認的父類是所有類默認的父類是objectobject類,類, objectobject類類有一個方法:有一個方法: public string tostring()public string tostring() 一個對象通過調用該方法可以獲得該對象的字符串表示。一個對象通過調用該方法可以獲得該對象的字符串表示。一個對象調用一個對象調用tostring()tostring()方法返回的字符串的一般形式為:方法返回的字符串的一般形式為:創建對象的類的名字創建對象的類

14、的名字 對象的引用的字符串表示對象的引用的字符串表示實際需要中子類重寫實際需要中子類重寫tostringtostring方法方法如如java.util.date java.util.date 類已重寫,輸出需要的時間格式類已重寫,輸出需要的時間格式用戶在自定義類中也建議改寫,用以輸出對象的成員變量用戶在自定義類中也建議改寫,用以輸出對象的成員變量例題例題9-69-6 22 /student .java public class student string name; public student() public student(string s) name=s; public string

15、 tostring() string oldstr=super.tostring(); /建議去掉 return oldstr+ni am a student,my name is +name; 23 import java.util.date; public class example9_6 public static void main(string args) date date=new date(); system.out.println( date.tostring(); student zhang=new student(zhang san); system.out.println

16、( zhang.tostring(); system.out.println(new student(li xiao).tostring(); 249.1.5 字符串與字符、字節數組字符串與字符、字節數組 1字符串與字符數組字符串與字符數組 string string 類的構造方法:類的構造方法: string(char)string(char)和和 string(charstring(char,int offsetint offset,int length)int length) 分別用字符數組中的全部字符和部分字符創建字符串對象分別用字符數組中的全部字符和部分字符創建字符串對象 strin

17、g string類提供了將字符串存放到數組中的方法:類提供了將字符串存放到數組中的方法:public void getchars(int start,int end,char c,int public void getchars(int start,int end,char c,int offset )offset ) 將字符串中的全部字符存放在一個字符數組中的方法:將字符串中的全部字符存放在一個字符數組中的方法:public char tochararray() public char tochararray() 例題例題9-79-725 public class example9_7 pu

18、blic static void main(string args) char a,b,c; string s=巴西足球隊擊敗德國足球隊巴西足球隊擊敗德國足球隊; a=new char2; s.getchars(5,7,a,0); system.out.println(a); b=new chars.length(); s.getchars(7,12,b,0); s.getchars(5,7,b,5); s.getchars(0,5,b,7); system.out.println(b); c=大家好,很高興認識大家大家好,很高興認識大家.tochararray(); for(char ch:

19、c) system.out.print(ch); 26 2字符串與字節數組字符串與字節數組 string(byte)用指定的字節數組構造一個字用指定的字節數組構造一個字符串對象。符串對象。 string(byte,int offset,int length) 用指定的字節數組的一部分,即從數組起始位置用指定的字節數組的一部分,即從數組起始位置offset開始取開始取length個字節構造一個字符串對象。個字節構造一個字符串對象。 public byte getbytes() 使用平臺默認的字符編碼,將當前字符串轉化使用平臺默認的字符編碼,將當前字符串轉化為一個字節數組。為一個字節數組。 pub

20、lic byte getbytes(string charsetname) 使用參數指定字符編碼,將當前字符串轉化為一使用參數指定字符編碼,將當前字符串轉化為一個字節數組。個字節數組。 例題例題9-827 public class example9_8 public static void main(string args) byte d=abc你我他.getbytes(); system.out.println(數組d的長度是(一個漢字占兩個字節):+d.length); string s=new string(d,3,2); /輸出:你 system.out.println(s); s=n

21、ew string(d,7,2); system.out.println(s); /輸出:他 289.1.6 正則表達式及字符串的替換與分解正則表達式及字符串的替換與分解 1 1正則表達式正則表達式 一個正則表達式是含有一些具有一個正則表達式是含有一些具有特殊意義字符的特殊意義字符的字符串,這字符串,這些特殊字符稱作些特殊字符稱作正則表達式中的元字符正則表達式中的元字符。 比如,比如,“dhello”dhello”中的中的dd就是有特殊意義的元字符,代就是有特殊意義的元字符,代表表0 0到到9 9中的任何一個。中的任何一個。方法方法: : public boolean matches(stri

22、ng regex)public boolean matches(string regex) 可以判斷當前字符可以判斷當前字符串對象是否和參數串對象是否和參數regexregex指定的正則表達式匹配。指定的正則表達式匹配。 參考表參考表9.19.1,表,表9.2 9.2 。 29 在正則表達式可以用方括號括起來若干個字符表示一個元字符,該元字符代表括號中任意一個字符。 s=“159abc”,則 “1abc”, ”5abc”, “9abc”都與s匹配其他見教材: p188 表9.2限定符 s=“w4”, 則 “abcd” “girl” “moon”等都與s 匹配其中: w 表示任意字符 4表示前面

23、的字符恰為4個30 2字符串的替換字符串的替換 方法:方法: public string replaceall(string regex,string public string replaceall(string regex,string replacement)replacement) 返回一個字符串,該字符串是當前字符串中所有和返回一個字符串,該字符串是當前字符串中所有和參數參數regexregex指定的正則表達式匹配的子字符串被參數指定的正則表達式匹配的子字符串被參數replacementreplacement指定的字符串替換后的字符串。指定的字符串替換后的字符串。不影響原不影響原字符

24、串字符串例如:例如:string string result=“result=“1212hellohello567567”.”.replaceallreplaceall(“d+”,“(“d+”,“你你好好”););那么那么resultresult就是:就是:“你好你好hellohello你好你好” ” “dd+ ” + ” 表示任意一個數字出現一次或表示任意一個數字出現一次或 多次多次例題例題9-99-9: 判斷一個字符串是否全有數字組成,并讓含有非數字判斷一個字符串是否全有數字組成,并讓含有非數字字符的字符串返回一個字符串(去掉非數字字符)字符的字符串返回一個字符串(去掉非數字字符)31pu

25、blic class example9_9 public static void main (string args ) string regex=1-90-9*.?0-9* ; string str1=12r34a5; string str2=123.45908; if(str1.matches(regex) system.out.println(str1+可以表示數字可以表示數字); else system.out.println(str1+不可以表示數字不可以表示數字); string result=str1.replaceall(“d+,); system.out.println(剔除

26、剔除+str1+的非數字字符得到的字符的非數字字符得到的字符串是:串是:); system.out.println(result); 32 if(str2.matches(regex) system.out.println(str2+可以表示數字); else system.out.println(str2+不可以表示數字); string result=str1.replaceall(d+,); system.out.println(剔除+str2+的非數字字符得到的字符串是:); system.out.println(result); 33 3字符串的分解字符串的分解 public str

27、ing split(string regex)public string split(string regex) 使用參數指定的正則表達式使用參數指定的正則表達式regexregex做為分隔做為分隔標記分解出其中的單詞,并將分解出的單詞存標記分解出其中的單詞,并將分解出的單詞存放在字符串數組中。放在字符串數組中。例如,對于字符串:例如,對于字符串: str=“1931str=“1931年年0909月月1818日晚,日本發動侵華戰爭,日晚,日本發動侵華戰爭,請記住這個日子!請記住這個日子!”; ; 使用正則表達式:string regex=d+; string regex=d+; 做為分隔標記分

28、解出str中的單詞: string digitword=str.split(regex); string digitword=str.split(regex); 那么,digitword0、digitword1和digitword2就分別是1931、09和18。 例題例題9-1034 import java.util.scanner; public class example9_10 public static void main (string args ) system.out.println(一行文本一行文本:); scanner reader=new scanner(system.in

29、); string str= reader.nextline(); /空格字符、數字和符號空格字符、數字和符號(!#$%&()*+,-./:;?_|)組成的正則表達式組成的正則表達式: string regex=sdppunct+; string words=str.split(regex); for(int i=0;iwords.length;i+) int m=i+1; system.out.println(單詞單詞+m+:+wordsi); 359.2 stringbufferstringbuffer類類 (變量串)(變量串) 9.2.1 stringbufferstringbu

30、ffer對象的創建對象的創建 string s = new string(我喜歡學習我喜歡學習); stringbuffer buffer = new stringbuffer(“stringbuffer buffer = new stringbuffer(“我喜歡學習我喜歡學習”);); buffer.append(buffer.append(數學數學);); 36 stringbuffer類有三個構造方法類有三個構造方法: 1stringbuffer(): 初始容量初始容量16個字符,多時個字符,多時自動增加自動增加 2stringbuffer(int size):初始容量:初始容量siz

31、e個字符,個字符,多時自動增加多時自動增加 3stringbuffer(string s) :初始容量:初始容量s個字符個字符再加再加16個字符,多時自動增加個字符,多時自動增加方法:方法:length():獲取字符序列的長度:獲取字符序列的長度capacity(): 實際容量實際容量 例題例題9-1137 public class example9_11 public static void main(string args) stringbuffer str=new stringbuffer(); str.append(大家好大家好); system.out.println(str:+st

32、r); system.out.println(length:+str.length(); system.out.println(capacity:+str.capacity(); str.append(我們大家都很愿意學習我們大家都很愿意學習java語言語言); system.out.println(str:+str); system.out.println(length:+str.length(); system.out.println(capacity:+str.capacity(); stringbuffer sb=new stringbuffer(hello); system.out.

33、println(length:+sb.length(); system.out.println(capacity:+sb.capacity(); 389.2.2 stringbufferstringbuffer類的常用方法類的常用方法 1 1stringbuffer append(string s):stringbuffer append(string s):將一個字符串對象追加到當前將一個字符串對象追加到當前stringbufferstringbuffer對象中對象中 stringbuffer append(int n)stringbuffer append(int n):將一個:將一個in

34、tint型數據轉化為字符串對象后再追型數據轉化為字符串對象后再追加到當前加到當前stringbufferstringbuffer對象中對象中 stringbuffer append(object o)stringbuffer append(object o):將一個將一個objectobject對象的字符串表示追加到對象的字符串表示追加到當前當前stringbufferstringbuffer對象中對象中 類似的方法還有:類似的方法還有: stringbuffer append(long n), stringbuffer append(boolean n),stringbuffer appen

35、d(long n), stringbuffer append(boolean n), stringbuffer append(float n), stringbuffer append(double n), stringbuffer append(float n), stringbuffer append(double n), stringbuffer append(char n) stringbuffer append(char n)392public chat charat(int n ):得到參數得到參數n指定的位置上的單個字符指定的位置上的單個字符 public void setcha

36、rat(int n ,char ch):將當前將當前stringbuffer對象實對象實體中的字符串位置體中的字符串位置n處的字符用參數處的字符用參數ch指定的字符替換指定的字符替換3stringbuffer insert(int index, string str) :將參數將參數str指定的字符指定的字符串插入到參數串插入到參數index指定的位置指定的位置 4public stringbuffer reverse() :將該對象實體中的字符翻轉將該對象實體中的字符翻轉5stringbuffer delete(int startindex, int endindex) :從當前從當前str

37、ingbuffer對象實體中的字符串中刪除一個子字符串對象實體中的字符串中刪除一個子字符串 其相關方法:其相關方法: deletecharat(int index) 刪除當前刪除當前stringbuffer對對象實體的字符串中象實體的字符串中index位置處的一個字符。位置處的一個字符。6. stringbuffer replace( int startindex ,int endindex, string str) :將當前將當前stringbuffer對象實體中的字符串的一個子字符串用參數對象實體中的字符串的一個子字符串用參數str指定的字符串替換指定的字符串替換 例題例題9-1240 p

38、ublic class example9_12 public static void main(string args) stringbuffer str= new stringbuffer(he like java); str.setcharat(0 ,w); str.setcharat(1 ,e); system.out.println(str); str.insert(2, all); system.out.println(str); int index=str.indexof(java); str.replace(index,str.length(),apple); system.ou

39、t.println(str); 419. 3 stringtokenizer類類 stringtokenizerstringtokenizer類類在在java.utiljava.util包中,有兩個常用的構造方包中,有兩個常用的構造方法:法:stringtokenizerstringtokenizer(string sstring s):):為字符串為字符串s s構造一個分析器。構造一個分析器。使用默認的分隔標記,即空格符(若干個空格被看做一個空格)、使用默認的分隔標記,即空格符(若干個空格被看做一個空格)、換行符、回車符、換行符、回車符、tabtab符、進紙符。符、進紙符。 stringto

40、kenizer(string s, string delim) stringtokenizer(string s, string delim):為字符串為字符串s s構造一個分析器。參數構造一個分析器。參數dilimdilim中的字符被作為分隔標記中的字符被作為分隔標記 。 42 stringtokenizer stringtokenizer對象稱作一個字符串分析器可以對象稱作一個字符串分析器可以使用下列方法:使用下列方法: nexttoken()nexttoken():逐個獲取字符串中的語言符號:逐個獲取字符串中的語言符號(單詞),字符串分析器中的負責計數的變量的值就(單詞),字符串分析器中

41、的負責計數的變量的值就自動減一自動減一 。 hasmoretokens()hasmoretokens():只要字符串中還有語言符號,即:只要字符串中還有語言符號,即計數變量的值大于計數變量的值大于0 0,該方法就返回,該方法就返回truetrue,否則返回,否則返回falsefalse。 counttokens()counttokens():得到分析器中計數變量的值。得到分析器中計數變量的值。 例題例題9-139-13:統計單詞:統計單詞43 import java.util.*; public class example9_13 public static void main(string

42、args) string s=we are stud,ents; stringtokenizer fenxi=new stringtokenizer(s, ,); int number=fenxi.counttokens(); while(fenxi.hasmoretokens() string str=fenxi.nexttoken(); system.out.println(str); system.out.println(還剩+fenxi.counttokens()+個單詞); system.out.println(s共有單詞:+number+個); 449.4 datedate類類 9

43、.4.1 構造構造datedate對象對象 datedate類在類在java.utiljava.util包中。使用包中。使用datedate類的無參數構造方法創類的無參數構造方法創建的對象可以獲取本地當前時間。建的對象可以獲取本地當前時間。 計算機參照時間:計算機參照時間: 1970.1.1 01970.1.1 0時時 ,格林威治時間,格林威治時間datedate的帶參數的構造方法:的帶參數的構造方法: date(long time)date(long time) : time time 毫秒毫秒例:例: date date1=new date(1000), date date1=new da

44、te(1000), 若北京時間,表示若北京時間,表示 1970.1.1 81970.1.1 8時時0000分分0101秒秒 date2=new date(-1000); date2=new date(-1000); 若北京時間,表示若北京時間,表示 1970.1.1 71970.1.1 7時時5959分分5959秒秒 45 systemsystem類中類中: : public static long currnttimesmillis(): public static long currnttimesmillis(): 若北京時區,獲取自若北京時區,獲取自1970.1.1 81970.1.1

45、8時時0000分分0000秒秒 到目前到目前的毫秒數的毫秒數 datedate對象默認時間格式:對象默認時間格式: 星期星期 月月 日日 小時小時 分分 秒秒 年年如如: : tue aug 04 08:59:32 cst 2009 tue aug 04 08:59:32 cst 2009 469.4.2 日期格式化日期格式化 使用使用java.utiljava.util包中的包中的dateformatdateformat的子類的子類simpledateformatsimpledateformat來來實現日期的格式化(按照自己的習慣)。實現日期的格式化(按照自己的習慣)。simpledatef

46、ormatsimpledateformat有一個常用構造方法:有一個常用構造方法: public simpledateformat(string pattern);public simpledateformat(string pattern);該構造方法可以用該構造方法可以用參數參數patternpattern指定的格式創建一個對象,該對象指定的格式創建一個對象,該對象調用:調用:public string format(date date)public string format(date date) 方法格式化時間對象方法格式化時間對象datedate。注:注:參數參數 patternpa

47、ttern中的日期格式符即被替換結果見教材中的日期格式符即被替換結果見教材p196p196其中的普通其中的普通asciiascii字符,必須用單引號括起來,字符,必須用單引號括起來,ruru patter=“time:yyyy-mm-dd” patter=“time:yyyy-mm-dd” 例題例題9-149-1447 import java.util.date; import java.text.simpledateformat; public class example9_14 public static void main(string args) date nowtime=new da

48、te(); system.out.println(現在的時間現在的時間:+nowtime); simpledateformat matter1= new simpledateformat( now time: y年年m月月d日日h時時m分分s秒秒); string timepattern=matter1.format(nowtime); system.out.println(timepattern); 48 simpledateformat matter2= new simpledateformat(g yyyy年年mmmd日日e hh時時mm分分ss秒秒z); timepattern=mat

49、ter2.format(nowtime); system.out.println(timepattern); long time=system.currenttimemillis(); system.out.println(從從+matter2.format (new date(6123210) + 至現在是:至現在是:n+time+毫秒毫秒); system.out.println(現在的時間現在的時間:+new date(time); 499. 5 calendar類類 1 1 calendarcalendar類類在在java.utiljava.util包中。使用包中。使用calendar

50、calendar類的類的staticstatic方法方法 getinstance()getinstance()可以初始化一個日歷對象,如:可以初始化一個日歷對象,如:calendar calendar= calendar.getinstance();calendar calendar= calendar.getinstance();2 calendar2 calendar對象可以調用方法:對象可以調用方法:public final void set(int year,int month,int date)public final void set(int year,int month,int

51、date)public final void set(int year,int month,int date,int public final void set(int year,int month,int date,int hour,int minute)hour,int minute)public final void set(int year,int month, int date, int public final void set(int year,int month, int date, int hour, int minute,int second)hour, int minut

52、e,int second)將日歷翻到任何一個時間將日歷翻到任何一個時間 50 3 calendar3 calendar對象常用方法對象常用方法 public int get(int field) public int get(int field) :可以獲取有關年份、可以獲取有關年份、月份、小時、星期等信息月份、小時、星期等信息 參數參數 fieldfield的值為的值為calendarcalendar中靜態常量,如:中靜態常量,如: calendar.month : calendar.month : 返回整數,若為返回整數,若為0 0,表示,表示1 1月月 calendar.yearcale

53、ndar.year calendar.day calendar.day calendar.day_of_week calendar.day_of_week 返回返回1 1表示星期日,表示星期日,2 2表示星期表示星期一,一,.7.7表示星期六表示星期六 public long gettimeinmillis() public long gettimeinmillis() :可以將時間表可以將時間表示為毫秒。示為毫秒。 例題例題9-15:1945年到年到1931年天數年天數51import java.util.*;import static java.util.calendar.*; /靜態導入

54、calendar類的靜態常量public class example9_15 public static void main(string args) calendar calendar=calendar.getinstance(); calendar.settime(new date(); string 年=string.valueof(calendar.get(year), 月=string.valueof(calendar.get(month)+1), 日=string.valueof(calendar.get(day_of_month), 星期=string.valueof(calen

55、dar.get(day_of_week)-1); int hour=calendar.get(hour_of_day), minute=calendar.get(minute), second=calendar.get(second); system.out.print(現在的時間是:); system.out.print(+年+年+月+月+日+日 + 星期+星期); system.out.println( +hour+時+minute+分+second+秒); 52 int year=1945,month=8,day=15; calendar.set(year,month-1,day); /

56、將日歷翻到將日歷翻到1945年年8月月15日日,注意注意7表示八月表示八月 system.out.print(year+年年+month+月月+day+日與日與); long time2=calendar.gettimeinmillis(); year=1931; month=9; day=18; calendar.set(year,month-1,day); /將日歷翻到將日歷翻到1931年年9月月18日日 system.out.print(year+年年+month+月月+day+日日); long time1=calendar.gettimeinmillis(); long 相隔天數相隔

57、天數=(time2-time1)/(1000*60*60*24); system.out.println(相隔相隔+相隔天數相隔天數+天天); 53例題例題9-16: 2012年年8月的日歷月的日歷import java.util.calendar;public class calendarbean string day; int year=2008,month=0; public void setyear(int year) this.year=year; public void setmonth(int month) this.month=month; public string getc

58、alendar() string a=new string42; calendar 日歷日歷=calendar.getinstance(); 日歷日歷.set(year,month-1,1); int 星期幾星期幾=日歷日歷.get(calendar.day_of_week)-1; int day=0; 54if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) day=31; if(month=4|month=6|month=9|month=11) day=30; if(month=2) if(year%4=0)&(

59、year%100!=0)|(year%400=0) day=29; else day=28; for(int i=0;i星期幾星期幾;i+) ai= ; for(int i=星期幾星期幾,n=1;i星期幾星期幾+day;i+) ai=string.valueof(n) ; n+; for(int i=星期幾星期幾+day;ia.length;i+) ai= ; return a; 55 public class example9_16 public static void main(string args) calendarbean cb=new calendarbean(); cb.sety

60、ear(2012); cb.setmonth(8); string a= cb.getcalendar(); /返回存放月中號碼的一維數組 char str=日一二三四五六.tochararray(); for(char c:str) system.out.printf(%3c,c); for(int i=0;ia.length;i+) /輸出數組a if(i%7=0) system.out.println(); /換行 system.out.printf(%4s,ai); 569. 6 math和和biginteger類類 9.6.1 mathmath類類 math類在類在java.lang包中。包中。ma

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論