欢迎来到天天文库
浏览记录
ID:42060776
大小:44.00 KB
页数:5页
时间:2019-09-07
《2010JAVA笔试题》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、写出下列程序运行的结果1.publicclassSignFunction{publicstaticvoidmain(Stringargs[]){intintx;intx=0;if(intx>0)System.out.println("Thesignof"+intx+"is+;");else{if(intx<0)System.out.println("Thesignof"+intx+"is-;");elseSystem.out.println("Thesignof"+intx+"is0;");}}}程序执行结果为:(0);2.//Sum.java源代
2、码如下:publicclassSum{publicstaticvoidmain(String[]args){intj=10;System.out.println("jis:"+j);calculate(j);System.out.println("Atlast,jis:"+j);}staticvoidcalculate(intj){for(inti=0;i<10;i++)j++;System.out.println("jincalculate()is:"+j);}}输出结果为:jis:(1)jincalculate()is:(2)Atlastjis
3、:(3)答案:jis:10jincalculate()is:20Atlast,jis:103.classUseArrayCopy{publicstaticvoidmain(Stringargs[]){inta[]={1,2,3,4,5,6,7};intb[]=newint[6];inti;System.arraycopy(a,1,b,2,3);for(i=0;i4、licstaticvoidmain(String[]args){intscore=86;chargrade;if(score>=90){grade='A';}elseif(score>=80){rade='B';}elseif(score>=70){grade='C';}elseif(score>=60){grade='D';}else{grade='E';}System.out.println("Grade="+grade);}}程序执行结果为:(Grade=B);5.处理除数为0的异常publicclassUserExceptionDemo1{5、publicstaticvoidmain(Stringargs[]){inta,b;try{a=10;b=0;intc=Divide(a,b);System.out.println(a+"/"+b+"="+c);}catch(ArithmeticExceptione){System.out.println("Dividedbyzeroerror!");}System.out.println("Aftertry-catch.");}staticintDivide(intx,inty){returnx/y;}}程序运行结果为:(Dividedbyzer6、oerror!Aftertry-catch.);五、程序阅读填空1、完成求n!的程序publicclassTest{publicstaticvoidmain(Stringargs[]){factorial(3);factorial(6);factorial(9);}static(1)factorial(intn){longm=1;for(intx=1;x<=n;(2))(3);System.out.println(n+"!="+m);}}六、编程题1.使用do~while循环,计算1至2000之间的所有奇数的和。解:staticintDivide(7、){inti=1;//i初始值为1intsum=0;do{sum+=i;i+=2;}while(i<2000);returnsum;}2.编程将字符串"Marywasa"改为"Maryhadabook."。StringBufferstr1=newStringBuffer("Marywasa");StringBufferstr2=str1.replace(5,10,"hadabook.");System.out.println(str2);3.计算1!+2!+3!+……+10!。inti;longj=1,s=0;for(i=1;i<=10;i++){8、j*=i;s+=j;}System.out.println(s);4.编写一个程序,用选择法对数组a[]={20,10,5
4、licstaticvoidmain(String[]args){intscore=86;chargrade;if(score>=90){grade='A';}elseif(score>=80){rade='B';}elseif(score>=70){grade='C';}elseif(score>=60){grade='D';}else{grade='E';}System.out.println("Grade="+grade);}}程序执行结果为:(Grade=B);5.处理除数为0的异常publicclassUserExceptionDemo1{
5、publicstaticvoidmain(Stringargs[]){inta,b;try{a=10;b=0;intc=Divide(a,b);System.out.println(a+"/"+b+"="+c);}catch(ArithmeticExceptione){System.out.println("Dividedbyzeroerror!");}System.out.println("Aftertry-catch.");}staticintDivide(intx,inty){returnx/y;}}程序运行结果为:(Dividedbyzer
6、oerror!Aftertry-catch.);五、程序阅读填空1、完成求n!的程序publicclassTest{publicstaticvoidmain(Stringargs[]){factorial(3);factorial(6);factorial(9);}static(1)factorial(intn){longm=1;for(intx=1;x<=n;(2))(3);System.out.println(n+"!="+m);}}六、编程题1.使用do~while循环,计算1至2000之间的所有奇数的和。解:staticintDivide(
7、){inti=1;//i初始值为1intsum=0;do{sum+=i;i+=2;}while(i<2000);returnsum;}2.编程将字符串"Marywasa"改为"Maryhadabook."。StringBufferstr1=newStringBuffer("Marywasa");StringBufferstr2=str1.replace(5,10,"hadabook.");System.out.println(str2);3.计算1!+2!+3!+……+10!。inti;longj=1,s=0;for(i=1;i<=10;i++){
8、j*=i;s+=j;}System.out.println(s);4.编写一个程序,用选择法对数组a[]={20,10,5
此文档下载收益归作者所有