




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、上机实践1 数据结构实验1 扫雷小游戏1答案:【代码1】: new LinkedList();【代码2】: list.add(blockij) ; 【代码3】: list.size(); 【代码4】: (Block)list.get(randomIndex);【代码5】: list.remove(randomIndex); 2模板代码 Block.javapublic class Block String name; int number; boolean boo=false; public void setName(String name) =name; public vo
2、id setNumber(int n) number=n; public int getNumber() return number; public String getName() return name; boolean isMine() return boo; public void setIsMine(boolean boo) this.boo=boo; LayMines.javaimport java.util.LinkedList;public class LayMines public void layMinesForBlock(Block block,int mineCount
3、) int row=block.length; int column=block0.length; LinkedList list=【代码1】 /创建空链表list for(int i=0;irow;i+) for(int j=0;j0) int size=【代码3】 / list返回节点的个数 int randomIndex=(int)(Math.random()*size); Block b=【代码4】 / list返回索引为randomIndex的节点中的数据 b.setName(雷); b.setIsMine(true); 【代码5】 /list删除索引值为randomIndex的节点
4、 mineCount-; for(int i=0;irow;i+) for(int j=0;jcolumn;j+) if(blockij.isMine() else int mineNumber=0; for(int k=Math.max(i-1,0);k=Math.min(i+1,row-1);k+) for(int t=Math.max(j-1,0);t=Math.min(j+1,column-1);t+) if(blockkt.isMine() mineNumber+; blockij.setName(+mineNumber); blockij.setNumber(mineNumber)
5、; BlockView.javaimport java.awt.*;public class BlockView extends Panel Label blockName; Button blockCover; CardLayout card; BlockView() card=new CardLayout(); setLayout(card); blockName=new Label(); blockCover=new Button(); add(cover,blockCover); add(name,blockName); public void setName(String name)
6、 blockName.setText(name); public String getName() return blockName.getText(); public void seeBlockName() card.show(this,name); validate(); public void seeBlockCover() card.show(this,cover); validate(); public Button getBlockCover() return blockCover; MineFrame.javaimport java.awt.*;import java.awt.e
7、vent.*;public class MineFrame extends Frame implements ActionListener Button reStart; Block block; BlockView blockView; LayMines lay; int row=10,colum=12,mineCount=22; int colorSwitch=0; Panel pCenter,pNorth; public MineFrame() reStart=new Button(重新开始); pCenter=new Panel(); pNorth=new Panel(); pNort
8、h.setBackground(Color.cyan); block=new Blockrowcolum; for(int i=0;irow;i+) for(int j=0;jcolum;j+) blockij=new Block(); lay=new LayMines(); lay.layMinesForBlock(block,mineCount); blockView=new BlockViewrowcolum; pCenter.setLayout(new GridLayout(row,colum); for(int i=0;irow;i+) for(int j=0;jcolum;j+)
9、blockViewij=new BlockView(); blockViewij.setName(blockij.getName(); pCenter.add(blockViewij); blockViewij.getBlockCover().addActionListener(this); reStart.addActionListener(this); pNorth.add(reStart); add(pNorth,BorderLayout.NORTH); add(pCenter,BorderLayout.CENTER); setSize(200,232); setVisible(true
10、); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); validate(); public void actionPerformed(ActionEvent e) Button source=(Button)e.getSource(); if(source!=reStart) int m=-1,n=-1; for(int i=0;irow;i+) for(int j=0;jcolum;j+) if(source=blockViewij.getBlo
11、ckCover() m=i; n=j; break; if(blockmn.isMine() for(int i=0;irow;i+) for(int j=0;j0) blockViewmn.seeBlockName(); else if(blockmn.getNumber()=0) for(int k=Math.max(m-1,0);k=Math.min(m+1,row-1);k+) for(int t=Math.max(n-1,0);t=Math.min(n+1,colum-1);t+) blockViewkt.seeBlockName(); if(source=reStart) for(
12、int i=0;irow;i+) for(int j=0;jcolum;j+) blockij.setIsMine(false); lay.layMinesForBlock(block,mineCount); for(int i=0;irow;i+) for(int j=0;jcolum;j+) blockViewij.setName(blockij.getName(); blockViewij.seeBlockCover(); blockViewij.getBlockCover().addActionListener(this); MineExample.javapublic class L
13、ayMineMainClass public static void main(String args) new MineFrame(); 实验2 排序与查找2模板代码 Book.javapublic class Book implements Comparable double price; String name; public void setPrice(double c) price=c; public double getPrice() return price; public void setName(String n) name=n; public String getName(
14、) return name; public int compareTo(Object object) Book bk=(Book)object; int difference=(int)(this.getPrice()-bk.getPrice()*10000); return difference; SortSearchMainClass.javaimport java.util.*;public class SortSearchMainClass public static void main(String args) LinkedList bookList=new LinkedList()
15、; String bookName=Java编程,XML基础,JSP基础,C+基础,J2ME编程,操作系统,数据库技术; double bookPrice=29,21,22,29,34,32,29; Book book=new BookbookName.length; for(int k=0;k=0) Book bk=(Book)bookList.get(m); System.out.println( +bk.getName()+(+bk.getPrice()+); bookList.remove(m); System.out.println(价钱相同); 实验3 使用TreeSet排序1答案
16、:【代码1】: new TreeSet();【代码2】: treeSet.add(stu); 【代码3】: tree.iterator(); 【代码4】: te.hasNext()【代码5】: (Student)te.next(); 2模板代码 Student.javapublic class Student implements Comparable String name; int score; Student(String name,int score) =name; this.score=score; public int compareTo(Object b) St
17、udent st=(Student)b; int m=this.score-st.score; if(m!=0) return m; else return 1; public int getScore() return score; public String getName() return name; StudentFrame.javaimport java.awt.*;import java.awt.event.*;import java.util.*;public class StudentFrame extends Frame implements ActionListener T
18、extArea showArea; TextField inputName,inputScore; Button button; TreeSet treeSet; StudentFrame() treeSet=【代码1】 /使用无参数构造方法创建treeSet showArea=new TextArea(); showArea.setFont(new Font(,Font.BOLD,20); inputName=new TextField(5); inputScore=new TextField(5); button=new Button(确定); button.addActionListen
19、er(this); Panel pNorth=new Panel(); pNorth.add(new Label(Name:); pNorth.add(inputName); pNorth.add(new Label(Score:); pNorth.add(inputScore); pNorth.add(button); add(pNorth,BorderLayout.NORTH); add(showArea,BorderLayout.CENTER); setSize(300,320); setVisible(true); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年智慧校园校园安全管理创新技术应用深度报告
- 2025年电商平台售后服务质量提升策略与实施路径报告
- 2025年新能源汽车废旧电池回收利用产业链上下游对接与合作报告
- 2025年职业教育产教融合项目资金申请中的职业教育国际化与本土化结合报告
- 2025年城市公共停车场建设社会稳定风险评估与历史文化保护报告
- 2025年文化产业园区的产业集聚与服务体系建设趋势分析报告
- 解析卷冀教版8年级下册期末试题及参考答案详解【B卷】
- 推拿治疗学考试题库附参考答案详解【夺分金卷】
- 2025年度输送泵租赁及现场技术服务合同
- 2025版大型设施设备定期检修劳务合同范本
- 新版2026统编版小学道德与法治三年级上册 第4课《 科技力量大》第1课时 科技改变生活和科技改变观念 教案设计(教案)
- 学会交流与沟通课件
- 2025年幼儿园教师大班数学工作总结样本(3篇)
- 铁路监理培训考试试题及答案
- 2025年毕节市农业发展集团有限公司招聘考试笔试试题(含答案)
- 供应链安全管理知识培训课件
- 供应链与贸易安全培训课件
- 牛鼻子引流技术
- 严禁燃放烟花炮竹课件
- 宫颈息肉课件
- (2025年标准)班组承包协议书
评论
0/150
提交评论