




已阅读5页,还剩33页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 Java初学者第一个小程序public class HelloWorld public static void main(String args) System.out.println(Hello World!);2 break练习public class TestBreak public static void main(String args) int stop = 4;for(int i=1; i=10; i+) if(i = stop) break; else System.out.println(i);3 continue练习public class TestContinute public static void main(String args) int con = 3;for(int i=1; i=5; i+) if(i = con) continue; else System.out.println(i);4 使用非递归的方法求Fibonacci数列第5个数是多少? /1, 1, 2, 3, 5, 8, 13public class TestFab public static void main(String args) System.out.println(method(5);public static long method(int index) if(index = 1 | index = 2) return 1;long f1 = 1L;long f2 = 1L;long f = 0;for(int i=0; iindex-2; i+) f = f1 + f2;f1 = f2;f2 = f;return f;5 使用递归求Fibonacci数列第5个数是多少?答:5public class TestFibonacci public static void main(String args) System.out.println(method(5);public static int method(int n) if(n = 1 | n = 2) return 1; else return method(n - 1) + method(n - 2);6 使用for循环求1! + 2! + 3! + . + 10! = 4037913;public class TestFor1 public static void main(String args) long f = 1L;long result = 0;for(int i=1; i=10; i+) f *= i;result += f;System.out.println(result);7 使用for循环求1 + 3 + 5 + . + 99 = 2500;public class TestFor2 public static void main(String args) int result = 0;for(int i=1; i=99; i+=2) result += i;System.out.println(result);8 if练习public class TestIF public static void main(String args) int i = 20;if(i20) System.out.println(i 20); else if (i40) System.out.println(i 60) System.out.println(asf); else System.out.println(kasjfla);9 使用递归求5! = 120;public class TestJC public static void main(String args) System.out.println(method(5);public static int method(int n) if(n = 1) return 1; else return n * method(n - 1);10 方法的调用举例public class TestMethod public static void main(String args) m1();m2(5);m3(4,5);System.out.println(m4(4, 6);public static void m1() /return;System.out.println(OK);public static void m2(int i) if(i3) return; else System.out.println(i);public static void m3(int i, int j) System.out.println(i + j);public static int m4(int i, int j) return i j ? i : j;11 switch问题举例,注意穿透现象public class TestSwitch public static void main(String args) int i = 3;switch(i) case 3 :System.out.println(A);break;case 5 :System.out.println(B);break;default:System.out.println(error); 12 while do while 举例,将1到9输出public class TestWhile public static void main(String args) int i = 1;while(i=9) System.out.print(i + );i+;System.out.println();i = 1;do System.out.print(i + );i+; while (i=9);13 求3维空间两点坐标之间的距离的平方class Point double x;double y;double z;Point(double x, double y, double z) this.x = x;this.y = y;this.z = z;void setX(double x) this.x = x;void setY(double y) this.y = y;void setZ(double z) this.z = z;double getDistance(Point p) return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z);public class TestPoint public static void main(String args) Point p1 = new Point(1.0, 2.0, 3.0);Point p2 = new Point(0.0, 0.0, 0.0);System.out.println(p1.getDistance(p2);p1.setY(1.0);System.out.println(p1.getDistance(new Point(1.0, 1.0, 3.0);14 重写equals方法判定两只猫是否相等public class TestEquals public static void main(String args) String s1 = a;String s2 = a;System.out.println(s1 = s2);System.out.println(s1.equals(s2);String s3 = new String(hello);String s4 = new String(hello);System.out.println(s3 = s4);System.out.println(s3.equals(s4);Cat c1 = new Cat(1, 2, 3);Cat c2 = new Cat(1, 2, 3);System.out.println(c1 = c2);System.out.println(c1.equals(c2); class Cat int color;int weight;int height;Cat(int color, int weight, int height) this.color = color;this.weight = weight;this.height = height;public boolean equals(Object obj) if(obj = null) return false; else if(obj instanceof Cat) Cat c = (Cat)obj;if(this.color = c.color & this.height = c.height & this.weight = c.weight) return true;return false;15 多态举例package com.dt;public class TestDt public static void main(String args) Cat c = new Cat(catname, yellow);Dog d = new Dog(dogname, black);Lady l1 = new Lady(l1, c);Lady l2 = new Lady(l2, d);l1.myPetEnjoy();l2.myPetEnjoy();class Animal private String name;Animal(String name) = name;public void enjoy() System.out.println(动物的叫声.);class Cat extends Animal private String eyeColor;Cat(String n, String eyeColor) super(n);this.eyeColor = eyeColor;public void enjoy() System.out.println(猫的叫声.);class Dog extends Animal private String furColor;Dog(String n, String furColor) super(n);this.furColor = furColor;public void enjoy() System.out.println(狗的叫声.);class Lady private String name;private Animal pet;Lady(String name, Animal pet) = name;this.pet = pet;public void myPetEnjoy() pet.enjoy();16 冒泡排序public class BubbleSort public static void main(String args) int a = new intargs.length;for(int i=0; iargs.length; i+) ai = Integer.parseInt(argsi);print(a);bubbleSort(a);print(a);private static void print(int a) for(int i=0; i=1; i-) for(int j=0; j aj+1) int temp = aj;aj = aj+1;aj+1 = temp;return a;17 使用数组数3退1:非面向对象的写法public class Count3Quit public static void main(String args) Boolean arr = new Boolean500;for(int i=0; i 1) if(arrindex = true) countNum +;if(countNum = 3) countNum = 0;arrindex = false;leftCount -;index +;if(index = arr.length) index = 0;for(int i=0; i 1) countNum +;if(countNum = 3) countNum = 0;kc.delete(k);k = k.right;class Kid int id;Kid(int id) this.id = id;Kid left;Kid right;class KidCircle int count = 0;Kid first, last;KidCircle(int n) for(int i=0; in; i+) add();void add() Kid k = new Kid(count);if(count = 0) first = k;last = k;k.left = k;k.right = k; else last.right = k;k.left = last;k.right = first;first.left = k;last = k;count +;void delete(Kid k) if(count = 0) return; else if(count = 1) first = last = null; else k.left.right = k.right;k.right.left = k.left;if(k = first) first = k.right; else if(k = last) last = k.left;count -;19 选择排序public class SelectionSort public static void main(String args) int a = new intargs.length;for(int i=0; iargs.length; i+) ai = Integer.parseInt(argsi);print(a);selectionSort(a);print(a);private static void print(int a) for(int i=0; ia.length; i+) System.out.print(ai + );System.out.println();private static void selectionSort(int a) int temp, k;for(int i=0; ia.length; i+) k = i;for(int j=k+1; j aj) k = j;if(k != i) temp = ai;ai = ak;ak = temp;20 简单运算器public class TestArgs public static void main(String args) double d1 = Double.parseDouble(args0);double d2 = Double.parseDouble(args2);double d = 0;if(args1.equals(+) d = d1 + d2; else if(args1.equals(-) d = d1 - d2; else if(args1.equals(x) d = d1 * d2; else if(args1.equals(/) d = d1 / d2; else System.out.println(error);System.out.println(d);21 数组拷贝练习public class TestArrayCopy public static void main(String args) int intArray = 1,2,3,4,5,6,7;int intArrayBak = new int3;System.arraycopy(intArray, 0, intArrayBak, 0, intArray.length);intArray21 = 100;for(int i=0; iintArray.length; i+) for(int j=0; jintArrayi.length; j+) System.out.print(intArrayij + );System.out.println(); 22 对日期排序public class TestDateSort public static void main(String args) Date days = new Date5;days0 = new Date(2006, 5, 4);days1 = new Date(2006, 7, 4);days2 = new Date(2008, 5, 4);days3 = new Date(2004, 5, 9);days4 = new Date(2004, 5, 4);Date d = new Date(2006, 7, 4);String str = String.valueOf(d);/str = d.toString();bubbleSort(days);for(int i=0; i=1;i-) for(int j = 0;j 0) Date temp = aj; aj=aj+1; aj+1=temp; return a; public static int binarySearch(Date days, Date d) if (days.length=0) return -1; int startPos = 0; int endPos = days.length-1; int m = (startPos + endPos) / 2; while(startPos 0) startPos = m + 1; if(pare(daysm) date.year ? 1 : year date.month ? 1 : month date.day ? 1 : day date.day ? -1 : 0; public String toString() return Year:Month:Day - + year + - + month + - + day; 23 二分法查找public class TestSearch public static void main(String args) int a = 1, 3, 6, 8, 9, 10, 12, 18, 20, 34 ;int i = 12;/System.out.println(search(a, i);System.out.println(binarySearch(a, i);public static int search(int a, int num) for(int i=0; ia.length; i+) if(ai = num) return i;return -1;public static int binarySearch(inta, int num) if (a.length=0) return -1; int startPos = 0; int endPos = a.length-1; int m = (startPos + endPos) / 2; while(startPos am) startPos = m + 1; if(num am) endPos = m -1; m = (startPos + endPos) / 2; return -1; 24 将字符串String s = 1,2;3,4,5;6,7,8;分解成一个double的二维数组public class ArrayParser public static void main(String args) double d;String s = 1,2;3,4,5;6,7,8;String sFirst = s.split(;);d = new doublesFirst.length;for(int i=0; isFirst.length; i+) /System.out.println(sFirsti);String sSecond = sFirsti.split(,);di = new doublesSecond.length;for(int j=0; jsSecond.length; j+) /System.out.println(sSecondj);dij = Double.parseDouble(sSecondj);for(int i=0; id.length; i+) for(int j=0; jdi.length; j+) System.out.println(dij);25 列出D:/A下边所以的文件及文件夹import java.io.*;public class FileList public static void main(String args) File f = new File(d:/A);System.out.println(f.getName();tree(f, 1);private static void tree(File f, int level) String str = ;for(int i=0; ilevel; i+) str += ;File childs = f.listFiles();for(int i=0; ichilds.length; i+) System.out.println(str + childsi.getName();if(childsi.isDirectory() tree(childsi, level + 1);26 枚举举例enum MyColor red3, green, blue;public class TestEnum public static void main(String args) MyColor m = MyColor.red3;long l1 = 3333333255252553L;int i = (int) l1;System.out.println(i);short s = 5; s = (short) (s + 2); s += 2; switch(m) case red3 :System.out.println(MyColor.red3);break;case green :System.out.println(B_green);break;case blue :System.out.println(C_blue);break;default :System.out.println(error);break;System.out.println(m.toString().intern()=red3);27 创建文件java.txtimport java.io.*;public class TestFile public static void main(String args) String seperator = File.separator;String fileName = naruto.txt;String directory = m1/m2;File f = new File(directory, fileName);if(f.exists() System.out.println(文件名是: + f.getAbsolutePath();System.out.println(文件大小是: + f.length(); else f.getParentFile().mkdirs();try f.createNewFile(); catch (IOException e) e.printStackTrace();public class TestString1 public static void main(String args) String s1 = hello;String s2 = world;String s3 = hello;System.out.println(s1 = s3); /trues1 = new String(hello);s2 = new String(hello);System.out.println(s1 = s2);System.out.println(s1.equals(s2);28 求下列String s = du*JG;中大写字母和小写字母,和其他个字符各多少个?public class TestString2 public static void main(String args) String s = du*JG;String lStr = abcdefghijklmnopqrstuvwxyz;String uStr = ABCDEFGHIJKLMNOPQRSTUVWXYZ;int lCount = 0;int uCount = 0;int oCount = 0;for(int i=0; is.length(); i+) char c = s.charAt(i);if(lStr.indexOf(c) != -1) lCount +; else if(uStr.indexOf(c) != -1) uCount +; else oCount +;System.out.print(lCount + + uCount + + oCount);29 求java这个字符串在下列字符串中出现的次数String s = 5454javaadsfjavaadfasfjavasdfajavaaadfjavaADFAjava545;public class TestString3 public static void main(String args) String s = 5454javaadsfjavaadfasfjavasdfajavaaadfjavaADFAjava545;int count = 0;while(s.indexOf(java) != -1) s = s.substring(s.indexOf(java) + java.length();count +;System.out.println(count);30 将文件复制到新的位置import java.io.*;public class FileCopy public static void main(String args) int b = 0; FileReader in = null; FileWriter out = null; try in = new FileReader(d:/share/java/HelloWorld.java); out = new FileWriter(d:/share/java/io/HW.java); while(b=in.read()!=-1) out.write(b); out.close(); in.close(); catch (FileNotFoundException e2) System.out.println(找不到指定文件); System.exit(-1); catch (IOException e1) System.out.println(文件复制错误); System.exit(-1); System.out.println(文件已复制); 31 缓冲区字节流举例import java.io.*;public class TestBufferStream1 public static void main(String args) try FileInputStream fis = new FileInputStream(E:/EclipseCode_MyEclipse/Chapter8/src/TestBufferStream1.java); BufferedInputStream bis = new BufferedInputStream(fis); int c = 0; System.out.println(bis.read(); System.out.println(bis.read(); bis.mark(100); for(int i=0;i=10 & (c=bis.read()!=-1;i+) System.out.print(char)c+ ); System.out.println(); bis.reset(); for(int i=0;i=10 & (c=bis.read()!=-1;i+) System.out.print(char)c+ ); bis.close(); catch (IOExce
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工业废弃物处理的技术与流程优化
- 工业废水处理技术与案例分析
- 工业安全风险评估与预警系统建设
- 工业废水处理及再利用技术分析
- 工业机器人及自动化生产线的应用实践
- 工业自动化中的资源整合与利用
- 工业设计新产品创意与实践
- 柴油储存安全管理制度
- 标准配送仓库管理制度
- 校内托管学校管理制度
- 敦煌壁画中的莲花图像
- 医院护理培训课件:《跌倒坠床PDCA分析》
- 国开《民法学(1)》形考任务1-4答案
- 热力发电厂课程设计说明书
- 阶梯轴的机械加工工艺过程卡片
- 特发性矮小病例分享
- 气体吸收操作-吸收塔结构认知(化工单元操作课件)
- 2023年副主任医师(副高)-中西医结合内科学(副高)考试参考题库附带答案
- 北京市海淀区八年级下学期期末考试语文试题
- 人工智能知到章节答案智慧树2023年复旦大学
- DB5206T16-2018梵净山茶叶加工场所基本条件
评论
0/150
提交评论