java語(yǔ)言程序設(shè)計(jì)基礎(chǔ)篇(第八版)第三章--機(jī)械工業(yè)出版報(bào)社--李娜譯PPT課件_第1頁(yè)
java語(yǔ)言程序設(shè)計(jì)基礎(chǔ)篇(第八版)第三章--機(jī)械工業(yè)出版報(bào)社--李娜譯PPT課件_第2頁(yè)
java語(yǔ)言程序設(shè)計(jì)基礎(chǔ)篇(第八版)第三章--機(jī)械工業(yè)出版報(bào)社--李娜譯PPT課件_第3頁(yè)
java語(yǔ)言程序設(shè)計(jì)基礎(chǔ)篇(第八版)第三章--機(jī)械工業(yè)出版報(bào)社--李娜譯PPT課件_第4頁(yè)
java語(yǔ)言程序設(shè)計(jì)基礎(chǔ)篇(第八版)第三章--機(jī)械工業(yè)出版報(bào)社--李娜譯PPT課件_第5頁(yè)
已閱讀5頁(yè),還剩64頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

.,1,Chapter3Selections,2,Motivations,IfyouassignedanegativevalueforradiusinListing2.1,ComputeArea.java,theprogramwouldprintaninvalidresult.Iftheradiusisnegative,youdontwanttheprogramtocomputethearea.Howcanyoudealwiththissituation?,.,3,Objectives,TodeclarebooleantypeandwriteBooleanexpressionsusingcomparisonoperators(3.2).ToprogramAdditionQuizusingBooleanexpressions(3.3).Toimplementselectioncontrolusingone-wayifstatements(3.4)ToprogramtheGuessBirthdaygameusingone-wayifstatements(3.5).Toimplementselectioncontrolusingtwo-wayifstatements(3.6).Toimplementselectioncontrolusingnestedifstatements(3.7).Toavoidcommonerrorsinifstatements(3.8).Toprogramusingselectionstatementsforavarietyofexamples(BMI,ComputeTax,SubtractionQuiz)(3.9-3.11).TogeneraterandomnumbersusingtheMath.random()method(3.9).Tocombineconditionsusinglogicaloperators(,5,ComparisonOperators,OperatorNamegreaterthan=greaterthanorequalto=equalto!=notequalto,6,Problem:ASimpleMathLearningTool,AdditionQuiz,Run,Thisexamplecreatesaprogramtoletafirstgraderpracticeadditions.Theprogramrandomlygeneratestwosingle-digitintegersnumber1andnumber2anddisplaysaquestionsuchas“Whatis7+9?”tothestudent.Afterthestudenttypestheanswer,theprogramdisplaysamessagetoindicatewhethertheansweristrueorfalse.,7,One-wayifStatements,if(boolean-expression)statement(s);,if(radius=0)area=radius*radius*PI;System.out.println(Thearea+forthecircleofradius+radius+is+area);,8,Note,9,SimpleifDemo,SimpleIfDemo,Run,Writeaprogramthatpromptstheusertoenteraninteger.Ifthenumberisamultipleof5,printHiFive.Ifthenumberisdivisibleby2,printHiEven.,10,Problem:GuessingBirthday,GuessBirthday,Theprogramcanguessyourbirthdate.Runtoseehowitworks.,11,MathematicsBasisfortheGame,19is10011inbinary.7is111inbinary.23is11101inbinary,12,TheTwo-wayifStatement,if(boolean-expression)statement(s)-for-the-true-case;elsestatement(s)-for-the-false-case;,13,if.elseExample,if(radius=0)area=radius*radius*3.14159;System.out.println(Theareaforthe“+“circleofradius+radius+is+area);elseSystem.out.println(Negativeinput);,14,MultipleAlternativeifStatements,15,Traceif-elsestatement,if(score=90.0)grade=A;elseif(score=80.0)grade=B;elseif(score=70.0)grade=C;elseif(score=60.0)grade=D;elsegrade=F;,Supposescoreis70.0,Theconditionisfalse,animation,16,Traceif-elsestatement,if(score=90.0)grade=A;elseif(score=80.0)grade=B;elseif(score=70.0)grade=C;elseif(score=60.0)grade=D;elsegrade=F;,Supposescoreis70.0,Theconditionisfalse,animation,17,Traceif-elsestatement,if(score=90.0)grade=A;elseif(score=80.0)grade=B;elseif(score=70.0)grade=C;elseif(score=60.0)grade=D;elsegrade=F;,Supposescoreis70.0,Theconditionistrue,animation,18,Traceif-elsestatement,if(score=90.0)grade=A;elseif(score=80.0)grade=B;elseif(score=70.0)grade=C;elseif(score=60.0)grade=D;elsegrade=F;,Supposescoreis70.0,gradeisC,animation,19,Traceif-elsestatement,if(score=90.0)grade=A;elseif(score=80.0)grade=B;elseif(score=70.0)grade=C;elseif(score=60.0)grade=D;elsegrade=F;,Supposescoreis70.0,Exittheifstatement,animation,20,Note,Theelseclausematchesthemostrecentifclauseinthesameblock.,21,Note,cont.,Nothingisprintedfromtheprecedingstatement.Toforcetheelseclausetomatchthefirstifclause,youmustaddapairofbraces:inti=1;intj=2;intk=3;if(ij)if(ik)System.out.println(A);elseSystem.out.println(B);ThisstatementprintsB.,22,CommonErrors,Addingasemicolonattheendofanifclauseisacommonmistake.if(radius=0);area=radius*radius*PI;System.out.println(Theareaforthecircleofradius+radius+is+area);Thismistakeishardtofind,becauseitisnotacompilationerrororaruntimeerror,itisalogicerror.Thiserroroftenoccurswhenyouusethenext-lineblockstyle.,Wrong,23,TIP,24,CAUTION,25,Problem:AnImprovedMathLearningTool,Thisexamplecreatesaprogramtoteachafirstgradechildhowtolearnsubtractions.Theprogramrandomlygeneratestwosingle-digitintegersnumber1andnumber2withnumber1number2anddisplaysaquestionsuchas“Whatis92?”tothestudent.Afterthestudenttypestheanswerintheinputdialogbox,theprogramdisplaysamessagedialogboxtoindicatewhethertheansweriscorrect.,SubtractionQuiz,26,Problem:BodyMassIndex,BodyMassIndex(BMI)isameasureofhealthonweight.Itcanbecalculatedbytakingyourweightinkilogramsanddividingbythesquareofyourheightinmeters.TheinterpretationofBMIforpeople16yearsorolderisasfollows:,ComputeBMI,27,Problem:ComputingTaxes,TheUSfederalpersonalincometaxiscalculatedbasedonthefilingstatusandtaxableincome.Therearefourfilingstatuses:singlefilers,marriedfilingjointly,marriedfilingseparately,andheadofhousehold.Thetaxratesfor2009areshownbelow.,28,Problem:ComputingTaxes,cont.,ComputeTax,if(status=0)/Computetaxforsinglefilerselseif(status=1)/Computetaxformarriedfilejointlyelseif(status=2)/Computetaxformarriedfileseparatelyelseif(status=3)/Computetaxforheadofhouseholdelse/Displaywrongstatus,29,LogicalOperators,OperatorName!notbreak;case1:computetaxesformarriedfilejointly;break;case2:computetaxesformarriedfileseparately;break;case3:computetaxesforheadofhousehold;break;default:System.out.println(Errors:invalidstatus);System.exit(0);,44,switchStatementFlowChart,45,switchStatementRules,switch(switch-expression)casevalue1:statement(s)1;break;casevalue2:statement(s)2;break;casevalueN:statement(s)N;break;default:statement(s)-for-default;,46,switchStatementRules,Thekeywordbreakisoptional,butitshouldbeusedattheendofeachcaseinordertoterminatetheremainderoftheswitchstatement.Ifthebreakstatementisnotpresent,thenextcasestatementwillbeexecuted.,switch(switch-expression)casevalue1:statement(s)1;break;casevalue2:statement(s)2;break;casevalueN:statement(s)N;break;default:statement(s)-for-default;,Thecasestatementsareexecutedinsequentialorder,buttheorderofthecases(includingthedefaultcase)doesnotmatter.However,itisgoodprogrammingstyletofollowthelogicalsequenceofthecasesandplacethedefaultcaseattheend.,47,Traceswitchstatement,switch(ch)casea:System.out.println(ch);caseb:System.out.println(ch);casec:System.out.println(ch);,Supposechisa:,animation,48,Traceswitchstatement,switch(ch)casea:System.out.println(ch);caseb:System.out.println(ch);casec:System.out.println(ch);,chisa:,animation,49,Traceswitchstatement,switch(ch)casea:System.out.println(ch);caseb:System.out.println(ch);casec:System.out.println(ch);,Executethisline,animation,50,Traceswitchstatement,switch(ch)casea:System.out.println(ch);caseb:System.out.println(ch);casec:System.out.println(ch);,Executethisline,animation,51,Traceswitchstatement,switch(ch)casea:System.out.println(ch);caseb:System.out.println(ch);casec:System.out.println(ch);,Executethisline,animation,52,Traceswitchstatement,switch(ch)casea:System.out.println(ch);caseb:System.out.println(ch);casec:System.out.println(ch);Nextstatement;,Executenextstatement,animation,53,Traceswitchstatement,switch(ch)casea:System.out.println(ch);break;caseb:System.out.println(ch);break;casec:System.out.println(ch);,Supposechisa:,animation,54,Traceswitchstatement,switch(ch)casea:System.out.println(ch);break;caseb:System.out.println(ch);break;casec:System.out.println(ch);,chisa:,animation,55,Traceswitchstatement,switch(ch)casea:System.out.println(ch);break;caseb:System.out.println(ch);break;casec:System.out.println(ch);,Executethisline,animation,56,Traceswitchstatement,switch(ch)casea:System.out.println(ch);break;caseb:System.out.println(ch);break;casec:System.out.println(ch);,Executethisline,animation,57,Traceswitchstatement,switch(ch)casea:System.out.println(ch);break;caseb:System.out.println(ch);break;casec:System.out.println(ch);Nextstatement;,Executenextstatement,animation,58,ConditionalOperator,if(x0)y=1elsey=-1;isequivalenttoy=(x0)?1:-1;(boolean-expression)?expression1:expression2TernaryoperatorBinaryoperatorUnaryoperator,59,ConditionalOperator,if(num%2=0)System.out.println(num+“iseven”);elseSystem.out.println(num+“isodd”);System.out.println(num%2=0)?num+“iseven”:num+“isodd”);,60,ConditionalOper

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論