习题答案第一章1填空题:(1)简单、面向对象、分布式、解释性、健壮性、安全性、体系结构中立、可移植性、高性能、多线程、动态性。(2)字节码、虚拟机(3)appletviewer(4)java(5)javac(6).java(7).class2程序运行题:(1)略(2)略第二章1.合法的标识符:aChar、String、NUM、_num、two22.publicclassWelcome{publicstaticvoidmain(Stringargs[]){Stringname;name=Lily;System.out.println(Welcometo+name+'swebsite!);}}3.publicclassTriangular{publicstaticvoidmain(Stringargs[]){doublea=5.0,b=12.0,c;c=Math.sqrt(a*a+b*b);System.out.println(c);}}289附录CJava关键字4.结果如下所示:5.判断下列代码段中a的值:inta=1,b=4;if(ab&&a++8)//此时a=1........if(ab&a++8)//此时a=2........if(ab||a++8)//此时a=2........if(ab|a++8)//此时a=36.代码为:publicclassCalculation{publicstaticvoidmain(Stringargs[]){inta,b,c;a=34;b=82;System.out.println(3×16=+a);System.out.println(8÷4=+b);}}7.应如下改正:实例变量必须要通过类创建的实例来访问。应改为publicclassFindFault1{publicintx=0;publicintxMethod(){returnx;}publicstaticvoidmain(Stringargs[]){FindFault1instance=newFindFault1();instance.x=1;System.out.println(instance.xMethod());}}(2)publicclassFindFault{publicstaticvoidmain(Stringargs[]){for(inti=1;i101;i++){290Java程序设计if(i%5==0|i%3==0)System.out.print(+i);}}}8.代码如下:classPo{doublepower(doublex,intn){if(n1){returnx*power(x,n-1);}else{if(n0){return1.0/power(x,-n);}elseif(n==0)return1;elsereturnx;}}}publicclassPowerCalculation{publicstaticvoidmain(Stringargs[]){Pop=newPo();System.out.println(4的五次幂是:+p.power(4.0,5));System.out.println(5的-3次幂是:+p.power(5.0,-3));System.out.println(7.5的3次幂是:+p.power(7.5,3));}}第三章1.程序设计题:(1)publicclassJudgeRandomNumber{publicstaticvoidmain(Stringargs[]){inta,b;a=(int)(Math.random()*100);//Math.random()会产生一个0到1之间的值for(inti=1;i25;i++){b=(int)(Math.random()*100);if(ab)System.out.println(a+islargethan+b+!);elseif(a==b)System.out.println(a+equals+b+!);elseSystem.out.println(a+issmallerthan+b+!);a=b;}}291附录CJava关键字}3、程序如下:classEx3{publicstaticvoidmain(Stringargs[]){intweek=0;System.out.println(请输入1~7中的一个:);try{week=System.in.read();}catch(Exceptione){}switch(week){case'1':System.out.println(Monday);break;case'2':System.out.println(Tuesday);break;case'3':System.out.println(Wednesday);break;case'4':System.out.println(Thursday);break;case'5':System.out.println(Friday);break;case'6':System.out.println(Saturday);break;case'7':System.out.println(Sunday);break;default:System.out.println(输入数值非法);}}}4、代码如下:publicclassPrintNumber2{publicstaticvoidmain(Stringargs[]){for(inti=1;i101;i++){System.out.print(i+);if(i%10==0)System.out.println();}}}5、publicclassPrintPrime{publicstaticvoidmain(Stringargs[]){292Java程序设计inti;a:for(i=2;i100;i++){for(intj=2;j9;j++){if(i%j==0){if(i!=j)continuea;elsebreak;}}System.out.print(+i);}}}6.代码如下:(1)publicclassPrintGra1{publicstaticvoidmain(Stringargs[]){inti,j,k;for(i=0;i8;i++){for(j=0;j=i+1;j++)System.out.print();for(k=i;k8;k++)System.out.print(#);System.out.println();}}}(2)publicclassPrintGra2{publicstaticvoidmain(Stringargs[]){inti,j,k;for(i=0;i8;i++){for(j=0;j=i+1;j++)System.out.print();for(k=i;k15-i;k++)System.out.print(#);System.out.println();}}}7.代码如下:publicclassFibonacci{publicstaticvoidmain(Stringargs[]){intf[]=newint[16];inti;for(i=0;i15;i++){if(i==0||i==1)f[i+1]=1;elsef[i+1]=f[i]+f[i-1];293附录CJava关键字}for(i=0;i15;i++){intk=i+1;System.out.println(F[+k+]=+f[k]);}}}第四章1.判断题(1)(2)错误,一个数组下标应为整数或整数表达式。(3)错误,数组声明不为数组预分配空间,只是定义了数组名。(4)错误,要访问数组的某一元素,需要指明数组名和下标。(5)错误,应声明:intp[]=newint[100];(6)错误,可以用一个整型数组的自动初始化来完成,而不用使用for语句。(7)错误,不规则数组一定是多维的。2.程序设计题1publicclassPrintMonth_day{2publicstaticvoidmain(Stringargs[]){3intMonth_day[]={31,28,31,30,31,30,31,31,30,31,30,31};4intmonth=0;5System.out.println(2月有28或29天);6System.out.println(8月有+Month_day[7]+天);7System.out.println(12月有+Month_day[11]+天);8}9}3.(1)1publicclassCharArray{2publicstaticvoidmain(Stringargs[]){3charcharArray[]={'W','e','l','c','o','m','e','',4't','o','','m','y','','W','e',5'b','s','i','t','e','!'};6for(inti=0;icharArray.length;i++){7System.out.print(charArray[i]);8}9}10}(2)字符数组就是一串字符的集合,用String实现比用字符数组实现要简单和直观得多。(4)1publicclassRecursivePrintArray{2staticvoidprintArray(intarray[],intsize){3if(size==0){4return;5}294Java程序设计6else{7printArray(array,size-1);8System.out.print(array[size-1]+);9}10}1112publicstaticvoidmain(Stringargs[]){13intintArray[]={1,2,3,4,5,6};14printArray(intArray,intArray.length);15}16}(5)1publicclassReversePrintArray{2staticvoidreversePrintArray(intarray[],intsize){3if(size==0){4return;5}6else{7System.out.print(array[size-1]+);8reversePrintArray(array,size-1);9}10}1112publicstaticvoidmain(Stringargs[]){13intintArray[]={1,2,3,4,5,6};14reversePrintArray(intArray,intArray.length);15}16}(6)1publicclassRecursiveSelectionSort{2staticvoidselectionSort(intarray[],intsize){3intvalue,index=array.length-size,i;4if(size==1){5return;6}7else{8value=array[array.length-size];9for(i=array.length-size;iarray.length;i++){10if(valuearray[i]){11value=array[i];12index=i;13}14}15swap(array,array.length-size,index);16selectionSort(array,size-1);17}18}1920staticvoidswap(intarray[],inti,intj){295附录CJava关键字21inttemp;22temp=array[i];23array[i]=array[j];24array[j]=temp;25}2627publicstaticvoidmain(Stringargs[]