版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、第7章 常用實用類實驗1 比較日期的大小2模板代碼 DateExample.javaimport java.util.*;public class DateExample public static void main(String args ) Scanner read=new Scanner(System.in); System.out.println(輸入第一個日期的年份:); int yearOne=read.nextInt( ); System.out.println(輸入該年的月份:); int monthOne=read.nextInt( ); System.out.printl
2、n(輸入該月份的日期:); int dayOne=read.nextInt( ); System.out.println(輸入第二個日期的年份:); int yearTwo=read.nextInt( ); System.out.println(輸入該年的月份:); int monthTwo=read.nextInt( ); System.out.println(輸入該月份的日期:); int dayTwo=read.nextInt( ); Calendar calendar = Calendar.getInstance(); /初始化日歷對象 calendar.set(yearOne,mon
3、thOne,dayOne); / 將calendar的時間設置為yearOne年monthOne月dayOne日 long timeOne=calendar.getTimeInMillis(); / calendar表示的時間轉(zhuǎn)換成毫秒 calendar.set(yearTwo,monthTwo,dayTwo); / 將calendar的時間設置為yearTwo年monthTwo月dayTwo日 long timeTwo=calendar.getTimeInMillis(); / calendar表示的時間轉(zhuǎn)換成毫秒 Date date1=new Date(timeOne); / 用timeO
4、ne作為參數(shù)構造date1 Date date2=new Date(timeTwo); / 用timeTwo作為參數(shù)構造date2 if(date2.equals(date1) System.out.println(兩個日期的年、月、日完全相同); else if(date2.after(date1) System.out.println(您輸入的第二個日期大于第一個日期); else if(date2.before(date1) System.out.println(您輸入的第二個日期小于第一個日期); long 相隔天數(shù)=(timeTwo-timeOne)/(1000*60*60*24);
5、 / 計算兩個日期相隔天數(shù) System.out.printf(%d年%d月%d日和%d年%d月%d日相隔%d天,yearOne,monthOne,dayOne, yearTwo,monthTwo,dayTwo, 相隔天數(shù)); 實驗2 隨機布雷2模板代碼 MineExample.javaimport java.util.*;class Block String name; boolean boo=false; public void setName(String name) =name;/ 將參數(shù)name傳值給成員變量 public String getName() retu
6、rn name; / 返回成員變量name boolean isMine( ) return boo; / 返回成員變量boo public void setBoolean(boolean boo) this.boo=boo; / 將參數(shù)boo傳值給成員變量boo public class MineExample public static void main(String args ) int mine=25; Block block =new Block88; for(int i=0;i8;i+) for(int j=0;j8;j+) blockij=new Block( ); Linke
7、dList list=new LinkedList(); / 創(chuàng)建list for(int i=0;i8;i+) for(int j=0;j=0) int size=list.size(); / 返回list中節(jié)點個數(shù) int randomIndex=(int)(Math.random( )*size); Block b=list.get(randomIndex); / 返回list中索引值為randomIndex的節(jié)點中的對象 b.setName(); b.setBoolean(true); list.remove(randomIndex); / list刪除索引值為randomIndex的
8、節(jié)點 mine-; for(int i=0;i8;i+) for(int j=0;j8;j+) if(blockij.isMine() ) else int mineNumber=0; for(int k=Math.max(i-1,0);k=Math.min(i+1,7);k+) for(int t=Math.max(j-1,0);t=Math.min(j+1,7);t+) if(blockkt.isMine() ) mineNumber+; blockij.setName(+mineNumber); for(int i=0;i8;i+) for(int j=0;j8;j+) System.o
9、ut.printf(%2s,blockij.getName() ); System.out.printf(%n); 實驗3 使用TreeSet排序2模板代碼 TreeSetExample.javaimport java.util.*;public class TreeSetExample public static void main(String args ) TreeSet mytree=new TreeSet(); for(int i=0;i5;i+) Scanner read=new Scanner(System.in); System.out.println(學生的姓名:); Str
10、ing name=read.nextLine( ); System.out.println(輸入分數(shù)(整數(shù)):); int score=read.nextInt( ); mytree.add(new Student(score,name); / 向mytree添加Student對象 Iterator te=mytree.iterator();/ mytree返回Iterator對象 while(te.hasNext() / 判斷te中是否存在元素 Student stu=te.next(); / 返回te中的下一個元素 System.out.println(++ +stu.en
11、glish); class Student implements Comparable int english=0; String name; Student(int e,String n) english=e;name=n; public int compareTo(Object b) Student st=(Student)b; return (this.english-st.english); 實驗4 使用TreeMap排序2模板代碼 TreeMapExample.javaimport java.util.*;class MyKey implements Comparable int n
12、umber=0; MyKey(int number) this.number=number; public int compareTo(Object b) MyKey st=(MyKey)b; if(this.number-st.number)=0) return -1; else return (this.number-st.number); class Student String name=null; int englishScore,mathScore; Student(int e,int m,String name) englishScore=e; mathScore=m; this
13、.name=name; public class TreeMapExample public static void main(String args ) TreeMap treemap1=new TreeMap(); TreeMap treemap2=new TreeMap(); TreeMap treemap3=new TreeMap(); for(int i=1;i=5;i+) Scanner read=new Scanner(System.in); System.out.println(學生的姓名:); String name=read.nextLine( ); System.out.
14、println(輸入英語分數(shù)(整數(shù)):); int englishScore=read.nextInt( ); System.out.println(輸入數(shù)學分數(shù)(整數(shù)):); int mathScore=read.nextInt( ); Student stu=new Student(englishScore,mathScore,name); treemap1.put(new MyKey(stu.englishScore),stu); / 向treemap1添加“鍵/值”對,其中值為stu,要求按英語成績排序 treemap2.put(new MyKey(stu.mathScore),stu
15、); / 向treemap2添加“鍵/值”對,其中值為stu,要求按數(shù)學成績排序 treemap3.put(new MyKey(stu.englishScore+stu.mathScore),stu); / 向treemap3添加“鍵/值”對,其中值為stu,要求按總分成績排序 System.out.println(按英語成績排序:); Collection collection=treemap1.values(); / treemap1返回實現(xiàn)Collection接口的對象 Iterator iter=collection.iterator(); / collection返回的Iterato
16、r while(iter.hasNext() / 判斷iter中是否存在元素 Student te =iter.next(); / 返回iter中的下一個元素 System.out.printf(姓名:%s,英語:%d,數(shù)學:%dn,,te.englishScore,te.mathScore); System.out.println(按數(shù)學成績排序:); collection=treemap2.values(); / treemap2返回實現(xiàn)Collection接口的對象 iter=collection.iterator(); / collection返回的Iterator while(iter.hasNext() / 判斷iter中是否存在元素 Student te =iter.next(); / 返回iter中的下一個元素 System.out.printf(姓名:%s,數(shù)學:%d,英語:%dn,,te.mathScore,te.englishScore); System.out.println(按總分排序:)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030中國電子煙行業(yè)監(jiān)管政策調(diào)整對市場格局影響分析報告
- 城市綠化土壤修復及污染治理方案
- 少兒美術教學培訓話術及課程設計案例
- 塔吊拆卸安全技術方案及作業(yè)規(guī)范
- 2025-2030燃料電池行業(yè)市場分析及發(fā)展趨勢研判
- 2025-2030照明設備制造行業(yè)技術創(chuàng)新市場需求政府扶持行業(yè)發(fā)展分析報告
- 2025-2030熱泵設備行業(yè)政策調(diào)研及技術革新投資咨詢規(guī)劃
- 2025-2030湘菜半成品在新零售渠道的布局分析
- 2025-2030消防逃生設備市場技術標準分析生產(chǎn)認證規(guī)劃建議
- 2025-2030消防救援機器人實戰(zhàn)能力評估與改進方向報告
- 銀行情緒與壓力管理課件
- 甲狀腺危象護理查房要點
- 《無人機飛行安全及法律法規(guī)》第3版全套教學課件
- 2025內(nèi)蒙古電力集團招聘筆試考試筆試歷年參考題庫附帶答案詳解
- 交通警察道路執(zhí)勤執(zhí)法培訓課件
- 十五五學校五年發(fā)展規(guī)劃(2026-2030)
- 洗浴員工協(xié)議書
- GB/T 17642-2025土工合成材料非織造布復合土工膜
- 清欠歷史舊賬協(xié)議書
- 乙肝疫苗接種培訓
- 心衰患者的用藥與護理
評論
0/150
提交評論