《Java语言程序设计基础篇》第10版 梁勇 著第七章练习题答案.docx
- 文档编号:11867339
- 上传时间:2023-04-06
- 格式:DOCX
- 页数:56
- 大小:29.92KB
《Java语言程序设计基础篇》第10版 梁勇 著第七章练习题答案.docx
《《Java语言程序设计基础篇》第10版 梁勇 著第七章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版 梁勇 著第七章练习题答案.docx(56页珍藏版)》请在冰豆网上搜索。
《Java语言程序设计基础篇》第10版梁勇著第七章练习题答案
《Java语言程序设计(基础篇)》(第10版梁勇著)
第七章练习题答案
7.1
publicclassExercise07_01{
/**Mainmethod*/
publicstaticvoidmain(String[]args){
//CreateaScanner
Scannerinput=newScanner(System.in);
//Getnumberofstudents
System.out.print("Enternumberofstudents:
");
intnumberOfStudents=input.nextInt();
double[]scores=newdouble[numberOfStudents];//Arrayscores
doublebest=0;//Thebestscore
//Readscoresandfindthebestscore
System.out.print("Enter"+numberOfStudents+"scores:
");
for(inti=0;i scores[i]=input.nextDouble(); if(scores[i]>best) best=scores[i]; } //Declareandinitializeoutputstring chargrade;//Thegrade //Assignanddisplaygrades for(inti=0;i if(scores[i]>=best-10) grade='A'; elseif(scores[i]>=best-20) grade='B'; elseif(scores[i]>=best-30) grade='C'; elseif(scores[i]>=best-40) grade='D'; else grade='F'; System.out.println("Student"+i+"scoreis"+ scores[i]+"andgradeis"+grade); } } } 7.1附加 publicclassExercise07_01Extra{ publicstaticvoidmain(Stringargs[]){ Scannerinput=newScanner(System.in); boolean[]isCovered=newboolean[99];//Defaultfalse //Readallnumbersandmarkcorrespondingelementcovered intnumber=input.nextInt(); while(number! =0){ if(number>=0&&number<=99) isCovered[number-1]=true; number=input.nextInt(); } //Checkifallcovered booleanallCovered=true;//Assumeallcovered for(inti=0;i<99;i++) if(! isCovered[i]){ System.out.print((i+1)+""); allCovered=false;//Findonenumberifnotcovered } //Displayresult if(allCovered) System.out.println("Theticketscoverallnumbers."); else System.out.println("arethemissingnumbers."); } } 7.2 publicclassExercise07_02{ publicstaticvoidmain(String[]args){ java.util.Scannerinput=newjava.util.Scanner(System.in); int[]num=newint[10]; for(inti=0;i<10;i++){ //Readanumber System.out.print( "Readanumber: "); num[i]=input.nextInt(); } //Displaythearray for(inti=9;i>=0;i--){ System.out.println(num[i]); } } } 7.2附加 publicclassExercise07_02Extra{ publicstaticvoidmain(String[]args){ Scannerinput=newScanner(System.in); System.out.print("Enterthecoordinatesofsixpoints: "); doublex[]=newdouble[6]; doubley[]=newdouble[6]; for(inti=0;i x[i]=input.nextDouble(); y[i]=input.nextDouble(); } doubletotal=0; for(inti=1;i total+=getArea(x[0],y[0],x[i],y[i],x[i+1],y[i+1]); } System.out.println("Thetotalareais"+total); } publicstaticdoublegetArea(doublex1,doubley1,doublex2,doubley2,doublex3,doubley3){ doubles1=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); doubles2=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); doubles3=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); doubles=(s1+s2+s3)/2; returnMath.sqrt(s*(s-s1)*(s-s2)*(s-s3)); } } 7.3 publicclassExercise07_03{ publicstaticvoidmain(String[]args){ Scannerinput=newScanner(System.in); int[]counts=newint[100]; System.out.print("Entertheintegersbetween1and100: "); //Readallnumbers intnumber=input.nextInt();//numberreadfromafile while(number! =0){ if(number<=100&&number>=0) counts[number-1]++; number=input.nextInt(); } //Displayresult for(inti=0;i<100;i++){ if(counts[i]>0) System.out.println((i+1)+"occurs"+counts[i] +((counts[i]==1)? "time": "times")); } } } 7.3附加 publicclassExercise07_03Extra{ publicstaticvoidmain(String[]args){ intcount=0;//Howmanysolutionsarefound? //Queenpositions int[]queens=newint[8];//queensareplacedat(i,queens[i]) for(inti=0;i<8;i++) queens[i]=-1;//-1indicatesthatnoqueeniscurrentlyplacedinthe //ithrow queens[0]=0;//Initially,placeaqueenat(0,0)inthe0throw //k-1indicatesthenumberofqueensplacedsofar //Wearelookingforapositioninthekthrowtoplaceaqueen intk=1; while(k>=0){ //Findapositiontoplaceaqueeninthekthrow intj=findPosition(k,queens); if(j<0){ queens[k]=-1; k--;//backtracktothepreviousrow }else{ queens[k]=j; if(k==7){ count++;//Onemoresolutionfound System.out.println("Solution"+count+": "); printResult(queens); }else{ k++; } } } System.out.println("Howmanysolutions? "+count); } publicstaticintfindPosition(intk,int[]queens){ intstart=queens[k]==-1? 0: queens[k]+1; for(intj=start;j<8;j++){ if(isValid(k,j,queens)) returnj;//(k,j)istheplacetoputthequeennow } return-1; } /**Returntrueifyouaqueencanbeplacedat(k,j)*/ publicstaticbooleanisValid(intk,intj,intqueens[]){ //Seeif(k,j)isapossibleposition //Checkjthcolumn for(inti=0;i if(queens[i]==j) returnfalse; //Checkmajordiagonal for(introw=k-1,column=j-1;row>=0&&column>=0;row--,column--) if(queens[row]==column) returnfalse; //Checkminordiagonal for(introw=k-1,column=j+1;row>=0&&column<=7;row--,column++) if(queens[row]==column) returnfalse; returntrue; } /**Printatwo-dimensionalboardtodisplaythequeens*/ publicstaticvoidprintResult(int[]queens){ //Displaytheoutput for(inti=0;i<8;i++){ for(intj=0;j System.out.print("|"); System.out.print("|Q|"); for(intj=queens[i]+1;j<8;j++) System.out.print("|"); System.out.println(); } } } 7.4 publicclassExercise07_04{ //Mainmethod publicstaticvoidmain(String[]args){ double[]scores=newdouble[100]; doublesum=0; intcount=0; doubleitem; java.util.Scannerinput=newjava.util.Scanner(System.in); do{ System.out.print("Enteranewscore: "); item=input.nextDouble(); if(item>=0){ scores[count]=item; sum+=item; count++; } }while(item>=0); System.out.println("countis"+count); doubleaverage=(sum)/count; intnumOfAbove=0; intnumOfBelow=0; for(inti=0;i if(scores[i]>=average){ numOfAbove++; }else{ numOfBelow++; } } System.out.println("Averageis"+average); System.out.println("Numberofscoresaboveorequaltotheaverage" +numOfAbove); System.out.println("Numberofscoresbelowtheaverage"+numOfBelow); } } 7.4附加 publicclassExercise07_04Extra{ publicstaticvoidmain(String[]args){ //Prompttheusertoenterastring java.util.Scannerinput=newjava.util.Scanner(System.in); System.out.print("Enterastring: "); Strings=input.nextLine(); int[]counts=count(s); for(inti=0;i<10;i++){ System.out.println("Digit"+i+"occurs"+counts[i]+ "times"); } } publicstaticint[]count(Strings){ int[]counts=newint[10]; for(inti=0;i if(Character.isDigit(s.charAt(i))){ counts[s.charAt(i)-'0']++; } } returncounts; } } 7.5 /* 方法: useanarraynumberstostorethedistinctvalues. readeachvaluefrominput,checkifitisalreadyinthearraynumbers, ifnot,additintoit. //Suggestedtemplate: publicclassExercise07_05{ publicstaticvoidmain(String[]args){ //numbersarraywillstoredistinctvalues,maximumis10 int[]numbers=newint[10]; //Howmanydistinctnumbersareinthearray intnumberOfDistinctValues=0; java.util.Scannerinput=newjava.util.Scanner(System.in); System.out.print("Entertennumbers: "); for(inti=0;i //Readaninput intvalue=input.nextInt(); 步骤1: Checkifvalueisinnumbers 步骤2: Ifvalueisnotinnumbers,additintonumbers 步骤3: IncrementnumberOfDistinctValues } 步骤4: Displayoutput } } 解法1: publicclassExercise07_05{ publicstaticvoidmain(String[]args){ int[]numbers=newint[30];//numbersarraywillstoredistinctvalues,maximumis10 intnumberOfDistinctValues=0;//Howmanydistinctnumbersareinthe //array java.util.Scannerinput=newjava.util.Scanner(System.in); System.out.print("Enter"+numbers.length+"numbers: "); for(inti=0;i //Readaninput intvalue=input.nextInt(); //Step1: Checkifvalueisinnumbers booleanisInTheArray=false; for(intj=0;j if(value==numbers[j]){ isInTheArray=true; } } //Step2: Ifvalueisnotinnumbers,additintonumbers if(! isInTheArray){ numbers[numberOfDistinctValues]=value; //Step3: IncrementnumberOfDistinctValues numberOfDistinctValues++; } } System.out.println("Thenumberofdistinctvaluesis" +numberOfDistinctValues); for(inti=0;i System.out.print(numbers[i]+""); } } 解法2: publicclassExercise07_05{ publicstaticvoidmain(String[]args){ int[]numbers=newint[30];//numbersarraywillstoredistinctvalues,maximumis10 intnumberOfDistinctValues=0;//Howmanydistinctnumbersareinthe //array java.util.Scannerinput=newjava.util.Scanner(System.in); System.out.print("Enter"+numbers.length+"numbers: "); for(inti=0;i //Readaninput intvalue=input.nextInt(); //Step1: Checkifvalueisinnumbers booleanisInTheArray=false; fo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java语言程序设计基础篇 Java语言程序设计基础篇第10版 梁勇 著第七章练习题答案 Java 语言程序设计 基础 10 第七 练习题 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)