publicclassMainClass{publicstaticvoidprintArray(Integer[] inputArray){for(Integer element : inputArray){System.out.printf("%s ", element);System.out.println();}}publicstaticvoidprintArray(Double[] inputArray){for(Double element : inputArray){System.out.printf("%s ", element);System.out.println();}}publicstaticvoidprintArray(Character[] inputArray){for(Character element : inputArray){System.out.printf("%s ", element);System.out.println();}}publicstaticvoidmain(String args[]){Integer[] integerArray ={1,2,3,4,5,6};Double[] doubleArray ={1.1,2.2,3.3,4.4,5.5,6.6,7.7};Character[] characterArray ={'H','E','L','L','O'};System.out.println("输出整型数组:");printArray(integerArray);System.out.println("\n输出双精度型数组:");printArray(doubleArray);System.out.println("\n输出字符型数组:");printArray(characterArray);}}
3.Java 实例 – 汉诺塔算法
publicclassMainClass{publicstaticvoidmain(String[] args){int nDisks =3;doTowers(nDisks,'A','B','C');}publicstaticvoiddoTowers(int topN,char from,char inter,charto){if(topN ==1){System.out.println("Disk 1 from "+ from +" to "+to);}else{doTowers(topN -1, from,to, inter);System.out.println("Disk "+ topN +" from "+ from +" to "+to);doTowers(topN -1, inter, from,to);}}}
4.Java 实例 – 斐波那契数列
publicclassMainClass{publicstaticvoidmain(String[] args){for(int counter =0; counter <=10; counter++){System.out.printf("Fibonacci of %d is: %d\n", counter,fibonacci(counter));}}publicstaticlongfibonacci(long number){if((number ==0)||(number ==1))return number;elsereturnfibonacci(number -1)+fibonacci(number -2);}}
publicclassFindareas{publicstaticvoid main (String[]agrs){Figure f=newFigure(10,10);Rectangle r=newRectangle(9,5);Figure figref;
figref=f;System.out.println("Area is :"+figref.area());
figref=r;System.out.println("Area is :"+figref.area());}}classFigure{double dim1;double dim2;Figure(double a ,double b){
dim1=a;
dim2=b;}Doublearea(){System.out.println("Inside area for figure.");return(dim1*dim2);}}classRectangleextendsFigure{Rectangle(double a,double b){super(a ,b);}Doublearea(){System.out.println("Inside area for rectangle.");return(dim1*dim2);}}
publicclassMain{publicstaticvoidmain(String[] args){int[] intary ={99,12,22,34,45,67,5678,8990};int no =5678;int i =0;boolean found =false;for(; i < intary.length; i++){if(intary[i]== no){
found =true;break;}}if(found){System.out.println(no +" 元素的索引位置在: "+ i);}else{System.out.println(no +" 元素不在数组中");}}}
9.Java 实例 – continue 关键字用法
publicclassMain{publicstaticvoidmain(String[] args){StringBuffer searchstr =newStringBuffer("hello how are you. ");int length = searchstr.length();int count =0;for(int i =0; i < length; i++){if(searchstr.charAt(i)!='h')continue;
count++;
searchstr.setCharAt(i,'h');}System.out.println("发现 "+ count
+" 个 h 字符");System.out.println(searchstr);}}
10.Java 实例 – 标签(Label)
publicclassMain{publicstaticvoidmain(String[] args){
outerLoop:for(int i =0; i <3; i++){
innerLoop:for(int j =0; j <3; j++){if(i ==1&& j ==1){break outerLoop;// 跳出外部循环}System.out.println("i: "+ i +", j: "+ j);}}}}
publicclassInvertedTriangle{publicstaticvoidmain(String[] args){//打印倒立的三角形for(int m =1; m <=4; m++){//打印空格for(int n =0; n <= m; n++){System.out.print(" ");}//打印*for(int x =1; x <=7-2*(m -1); x++){System.out.print("*");}System.out.println();}}}
5.Java 实例 – 打印平行四边形
publicclassParallelogram{publicstaticvoidmain(String[] args){//外层循环 每次打出一个*for(int i =1; i <=5; i++){//填充空格for(int j =1; j <=5- i; j++){System.out.print(" ");}//内层循环 每次打印一个*for(int k =1; k <=5; k++){System.out.print("*");}System.out.println();}}}
6.Java 实例 – 打印矩形
publicclassRect{publicstaticvoidmain(String[] args){//外层循环 每次输出一行*for(int i =1; i <=5; i++){System.out.print("*");//内层循环 每次输出一个*for(int j =1; j <=5; j++){System.out.print("*");}System.out.println();}}}
六.Java 文件操作
1.Java 实例 - 文件写入
/*
author by runoob.com
Main.java
*/importjava.io.*;publicclassMain{publicstaticvoidmain(String[] args){try{BufferedWriter out =newBufferedWriter(newFileWriter("runoob.txt"));
out.write("菜鸟教程");
out.close();System.out.println("文件创建成功!");}catch(IOException e){}}}
2.Java 实例 - 读取文件内容
importjava.io.*;publicclassMain{publicstaticvoidmain(String[] args){try{BufferedReader in =newBufferedReader(newFileReader("test.log"));String str;while((str = in.readLine())!=null){System.out.println(str);}System.out.println(str);}catch(IOException e){}}}