java語言程序設(shè)計(jì)第五章答案-Java語言程序設(shè)計(jì)(基礎(chǔ)篇)原書第十版課后習(xí)題第五章_第1頁(yè)
java語言程序設(shè)計(jì)第五章答案-Java語言程序設(shè)計(jì)(基礎(chǔ)篇)原書第十版課后習(xí)題第五章_第2頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余9頁(yè)可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

java語?程序設(shè)計(jì)第五章答案_Java語?程序設(shè)計(jì)(基礎(chǔ)篇)原書第?版課后習(xí)題第五章...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),會(huì)出現(xiàn)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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論