您好,欢迎访问三七文档
JAVA编程实例1、publicclassMyClass{privateintval1,val2;publicvoidmyFun(intx,inty){val1=x;val2=y;System.out.println(Thesumis:+(val1+val2));}publicstaticvoidmain(Stringarg[]){MyClassMyObj=newMyClass();MyObj.myFun(1,2);}}运行结果如下:Thesumis:32、publicclassMyArray{publicstaticvoidmain(String[]args){intmyArray[];//声明数组myArray=newint[10];//创建数组System.out.println(Index\t\tValue);for(inti=0;imyArray.length;i++)System.out.println(i+\t\t+myArray[i]);//证明数组元素默认初始化为0//myArray[10]=100;//将产生数组越界异常}}输出:50503、publicclassArrays{publicstaticvoidmain(String[]args){int[]a1={1,2,3,4,5};int[]a2;a2=a1;for(inti=0;ia2.length;i++)a2[i]++;for(inti=0;ia1.length;i++)System.out.println(a1[+i+]=+a1[i]);}}运行结果:a1[0]=2a1[1]=3a1[2]=4a1[3]=5a1[4]=64、publicclassArrayOfStringsDemo{publicstaticvoidmain(String[]args){String[]anArray={StringOne,StringTwo,StringThree};for(inti=0;ianArray.length;i++){System.out.println(anArray[i].toLowerCase());}}}运行结果:stringonestringtwostringthree5、publicclassArrayCopyDemo{publicstaticvoidmain(String[]args){char[]copyFrom={'d','e','c','a','f','f','e','i','n','a','t','e','d'};char[]copyTo=newchar[7];System.arraycopy(copyFrom,2,copyTo,0,7);System.out.println(newString(copyTo));}}运行结果:caffeine6、ex3_1–输入一个年份,判断它是不是闰年。–闰年:能被4整除但不能被100整除,或者能被400整除。importjava.io.*;publicclassex3_1{publicstaticvoidmain(String[]args)throwsIOException{intyear;booleanIsLeapYear;System.out.println(Entertheyear:);BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));year=(newInteger(in.readLine())).intValue();IsLeapYear=((year%4==0&&year%100!=0)||(year%400==0));if(IsLeapYear){System.out.print(year);System.out.println(isaleapyear);}else{System.out.print(year);System.out.println(isnotaleapyear);}}}7、输入两个整数比较大小importjava.io.*;publicclassex3_2{publicstaticvoidmain(String[]args)throwsIOException{intx,y;BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.println(Enterxandy:);x=(newInteger(in.readLine())).intValue();y=(newInteger(in.readLine())).intValue();if(x!=y)if(xy)System.out.println(xy);elseSystem.out.println(xy);elseSystem.out.println(x=y);}}8、已知一个学生的分数,给出其分数等级。90-100分为A级;80-89分为B级;70-79分为C级;60-69分为D级;0-59分为E级publicclassIfElseDemo{publicstaticvoidmain(String[]args){inttestscore=76;chargrade;if(testscore=90){grade='A';}elseif(testscore=80){grade='B';}elseif(testscore=70){grade='C';}elseif(testscore=60){grade='D';}else{grade='F';}System.out.println(Grade=+grade);}}或publicclassGrade{publicstaticchargradeLevel(doubleg){intn=(int)Math.floor(g/10);switch(n){case10:case9:return('A');case8:return('B');case7:return('C');case6:return('D');default:return('E');}}publicstaticvoidmain(String[]args){System.out.println(gradeLevel(100)=+gradeLevel(100));System.out.println(gradeLevel(95.5)=+gradeLevel(95.5));System.out.println(gradeLevel(88)=+gradeLevel(88));System.out.println(gradeLevel(72)=+gradeLevel(72));System.out.println(gradeLevel(68.5)=+gradeLevel(68.5));System.out.println(gradeLevel(60)=+gradeLevel(60));System.out.println(gradeLevel(59.5)=+gradeLevel(59.5));System.out.println(gradeLevel(35)=+gradeLevel(35));}}9、输入0~6之间的某一个整数,然后把它转换成星期输出。(0对应星期日)importjava.io.*;publicclassex3_3{publicstaticvoidmain(String[]args)throwsIOException{intday;BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));day=(newInteger(in.readLine())).intValue();switch(day){case0:System.out.println(Sunday);break;case1:System.out.println(Monday);break;case2:System.out.println(Tuesday);break;case3:System.out.println(Wednesday);break;case4:System.out.println(Thursday);break;case5:System.out.println(Friday);break;case6:System.out.println(Saturday);break;default:System.out.println(DayoutofrangeSunday..Saturday);break;}}}10、打印九九乘数表publicclassMultiTable{publicstaticvoidmain(String[]args){for(inti=1;i=9;i++){for(intj=1;j=i;j++)System.out.print(+i+*+j+=+i*j);System.out.println();}}}11、输入一个整数,输出它所有的因数importjava.io.*;publicclassex3_7{publicstaticvoidmain(String[]args)throwsIOException{intn,k;BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.println(Enterapositiveinteger:);n=(newInteger(in.readLine())).intValue();System.out.print(Number+n+Factors);for(k=1;k=n;k++)if(n%k==0)System.out.print(k+);System.out.println();}}12、计算数列1,2,…,10的和。publicclassex3_4{publicstaticvoidmain(String[]args){inti=1,sum=0;while(i=10){sum+=i;i++;}System.out.println(sum=+sum);}}13、输入一个整数,然后输出它的翻转形式importjava.io.*;publicclassex3_5{publicstaticvoidmain(String[]args)throwsIOException{intn,right_digit,newnum=0;BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.println(Enterthenumber:);n=(newInteger(in.readLine())).intValue();System.out.print(Thenumberinreverseorderis);do{right_digit=n%10;System.out.print(right_digit);n/=10;}while(n!=0);System.out.println();}}14、publicclassBreakTest{publicstaticvoidmain(Stringargs[]){Stringoutput=;inti;for(i=1;i=10;i++){if(i==5)break;//breaklooponlyifcount==5output+=i+;}output+=\nBrokeoutofloopati=+i;System.out.println(output);}}运行结果1234Brokeoutofloopati=515、打印2到9之间的偶数的平方,但是不包括偶数
本文标题:java程序实例
链接地址:https://www.777doc.com/doc-1861561 .html