java语言程序设计第五章答案-Java语言程序设计(基础篇)原书第十版课后习题第五章_第1页
java语言程序设计第五章答案-Java语言程序设计(基础篇)原书第十版课后习题第五章_第2页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java语⾔程序设计第五章答案_Java语⾔程序设计(基础篇)原书第⼗版课后习题第五章...importjava.util.Scanner;publicclassCode_Practice_5{publicstaticvoidmain(String[]args){//5.1Scannerinput=newScanner(System.in);System.out.print("Enteraninteger,theinputendsifitis0:");intnumber=input.nextInt();intcountP=0,countN=0;inttotal=0,count=0;if(number!=0){//如果写成while(true),会出现Unreachablecode的问题,原因没找到while(number!=0){if(number>countP++;elsecountN++;total+=number;number=input.nextInt();}doubleaverage=total/(countP+countN);System.out.println("Thenumberofpositivesis"+countP);System.out.println("Thenumberofnegativesis"+countN);System.out.println("Thetotalis"+total);System.out.println("Theaverageis"+average);}elseSystem.out.println("Nonumbersareenteredexcept0");//5.2finalintNUMBER_OF_QUESTIONS=5;//NumberofquestionsintcorrectCount=0;//Countthecorrectintcount=0;//CountthequestionslongstartTime=System.currentTimeMillis();Stringoutput="";//outputstringisinitiallyempty;Scannerinput=newScanner(System.in);while(count<NUMBER_OF_QUESTIONS){//Generatetworandomsingle-digitintegersintnum1=(int)(1+Math.random()*14);intnum2=(int)(1+Math.random()*14);System.out.print("Whatis"+num1+"+"+num2+"?");intanswer=input.nextInt();if(num1+num2==answer){System.out.println("Youarecorrect!");correctCount++;}elseSystem.out.println("Youransweriswrong.\n"+num1+"+"+num2+"shouldbe"+(num1+num2));count++;output+="\n"+num1+"+"+num2+"="+answer+((num1+num2==answer)?"correct":"wrong");}longendTime=System.currentTimeMillis();longtestTime=endTime-startTime;System.out.println("\nCorrectcountis"+correctCount+"\nTesttimeis"+testTime/1000+"seconds\n"+output);//5.3intkilo=1;doublepound=0;System.out.println("千克"+""+"磅");for(inti=0;i<199;i++){System.out.print(kilo);pound=kilo*2.2;System.out.printf("%12.1f\n",pound);kilo++;}//5.4intmiles=1;doublekilo=0;System.out.println("英⾥"+""+"千⽶");for(inti=0;i<10;i++){System.out.printf("%-9d",miles);kilo=miles*1.609;System.out.printf("%-7.3f\n",kilo);miles++;}//5.5intkilo1=1,pound2=20;doublepound1=1,kilo2=1;System.out.println("千克"+""+"磅"+""+"磅"+""+"千克");for(inti=0;i<199;i++){System.out.print(kilo1);pound1=kilo1*2.2;System.out.printf("%12.1f",pound1);for(intj=0;j<99;j++){System.out.printf("%6d",pound2);kilo2=pound2/2.2;System.out.printf("%8.2f\n",kilo2);pound2+=5;break;}kilo1++;}//5.6intmiles1=1,kilo2=20;doublekilo1=0,miles2=0;System.out.println("英⾥"+""+"千⽶"+""+"千⽶"+""+"英⾥");for(inti=0;i<10;i++){System.out.printf("%-9d",miles1);kilo1=miles1*1.609;System.out.printf("%-7.3f",kilo1);for(intj=0;j<10;j++){System.out.printf("%6d",kilo2);miles2=kilo2/1.609;System.out.printf("%13.3f\n",miles2);kilo2+=5;break;}miles1++;}//5.7inttuition=10000;intyear=0;inttotal=0;while(year<14){tuition*=1.05;year++;System.out.println(year+""+tuition);while(year>10){total+=tuition;break;}}System.out.println(total);//5.8Scannerinput=newScanner(System.in);System.out.print("Enterthenumberofstudent:");intnum=input.nextInt();doublehighestScore=0,higherScore=0;StringhighestName="",higherName="";for(intcount=0;count<num;count++){System.out.print("Enterthe"+(count+1)+"student\'sname:");Stringname=input.next();System.out.print("Enterthe"+(count+1)+"student\'sscore:");doublescore=input.nextDouble();if(score>highestScore&&score>higherScore){highestScore=score;highestName=name;}elseif(score<highestScore&&score>higherScore){higherScore=score;higherName=name;}}System.out.println("Thebeststudentis"+highestName+"andthesocreis"+highestScore);System.out.println("Thesecondstudentis"+higherName+"andthesocreis"+higherScore);//5.9Scannerinput=newScanner(System.in);//PrompttheusertoenterthenumberofstudentsSystem.out.print("Enterthenumberofstudents:");intnumberOfStudents=input.nextInt();System.out.print("Enterastudentname:");Stringstudent1=input.next();System.out.print("Enterastudentscore:");doublescore1=input.nextDouble();System.out.print("Enterastudentname:");Stringstudent2=input.next();System.out.print("Enterastudentscore:");doublescore2=input.nextDouble();//Makesurethatstudent1isforthehighest//andstudent2isforthesecondhighestif(score1<score2){//SwapStringtempString=student1;doubletempScore=score1;student1=student2;score1=score2;student2=tempString;score2=tempScore;}for(inti=0;i<numberOfStudents-2;i++){System.out.print("Enterastudentname:");Stringstudent=input.next();System.out.print("Enterastudentscore:");doublescore=input.nextDouble();if(score>score1){student2=student1;//student1nowisthesecondhighestscore2=score1;student1=student;//newstudentbecomesthehighestscore1=score;}elseif(score>score2){student2=student;//newstudentbecomesthesecondhighestscore2=score;}}System.out.println("Toptwostudents:");System.out.println(student1+"'sscoreis"+score1);System.out.println(student2+"'sscoreis"+score2);//5.10intcount=0;for(inti=100;i<1000;i++){if(i%5==0&&i%6==0)System.out.print((++count%10!=0)?i+"":i+"\n");}//5.11intcount=1;for(inti=100;i<1000;i++){if(i%5==0^i%6==0)System.out.print((count++%10!=0)?i+"":i+"\n");}//5.12intn=while(Math.pow(n,2)<=12000)n++;System.out.println("Thisnumberis"+n);//5.13intn=while(n*n*n<12000)n++;System.out.println("Thisnumberis"+(n-1));//5.14Scannerinput=newScanner(System.in);System.out.print("Enterthefirstnumber:");intnum1=input.nextInt();System.out.print("Enterthesecondnumber:");intnum2=input.nextInt();intd=(num1<num2)?num1:num2;for(;d>=1;d--){if((num1%d==0)&&(num2%d==0))break;}System.out.println("GCDof"+num1+"and"+num2+"is"+d);//5.15intcount=1;for(inti='!';i<'~';i++)System.out.print((count++%10!=0)?(char)i+"":(char)i+"\n");//5.16Scannerinput=newScanner(System.in);System.out.print("Enterainteger:");intnumber=input.nextInt();intfac=2;//initializethefactorSystem.out.println("Thefactorsfor"+number+"is");while(fac<=number){if(number%fac==0){number/=fac;System.out.print(fac+"");}elsefac++;}//5.17Scannerinput=newScanner(System.in);//PrompttheusertoenterthenumberoflinesSystem.out.print("Enterthenumberoflines:");intnumberOfLines=input.nextInt();if(numberOfLines<1||numberOfLines>15){System.out.println("Youmustenteranumberfrom1to15");System.exit(0);}//Printlinesfor(introw=1;row<=numberOfLines;row++){for(intcolumn=1;column<=numberOfLines-row;column++)System.out.print("");for(intnum=row;num>=1;num--)System.out.print((num>=10)?""+num:""+num);for(intnum=2;num<=row;num++)System.out.print((num>=10)?""+num:""+num);//StartanewlineSystem.out.println();}//5.20intcount=1;//countthenumberofprimeintnum=2;//numberfrom2to1000;booleanisPrime=true;//thenumberisprimeofnotSystem.out.println("Thenumberisfrom2to1000.");while(num<=1000){isPrime=true;for(inti=2;i<=num/2;i++){if(num%i==0){isPrime=false;break;}}if(isPrime)System.out.print((count++%8!=0)?num+"":num+"\n");num++;}//5.21Scannerinput=newScanner(System.in);//PromtentertheamountofloanSystem.out.print("Enterloanamount:");doubleloanAmount=input.nextDouble();//PromtentertheyearofloanSystem.out.print("Enterloanyearasainteger:");intyearOfLoan=input.nextInt();System.out.print("InterestRate");System.out.print("\tMonthlyPayment");System.out.println("\t\tTotalPayment");//\ttake8positionfor(doubleannualInterestRate=5.0;annualInterestRate<=8.0;annualInterestRate+=1.0/8){//ObtainmonthlyinterestratedoublemonthlyInterestRate=annualInterestRate/1200;//ComputemortgagedoublemonthlyPayment=loanAmount*monthlyInterestRate/(1-(Math.pow(1/(1+monthlyInterestRate),yearOfLoan*12)));doubletotalPayment=monthlyPayment*yearOfLoan*12;//DisplayresultsSystem.out.printf("%5.3f%c%20.2f%20.2f\n",annualInterestRate,'%',monthlyPayment,totalPayment);}//5.23finalintN=50000;doublebackwark=0,forward=0;doublesum1=0,sum2=0;for(inti=1;i<=N;i++)sum1+=1.0/i;for(inti=N;i>=1;i--)sum2+=1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论