




已閱讀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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 長(zhǎng)江中游不同中稻品種直播栽培產(chǎn)量和抗倒性差異及機(jī)理研究
- β受體阻滯劑治療創(chuàng)傷性腦損傷的臨床效果研究
- 碳排放約束下農(nóng)產(chǎn)品流通效率評(píng)價(jià)及提升路徑研究
- 環(huán)管內(nèi)超臨界CO2擬臨界區(qū)傳熱特性研究
- 基于相變膨脹的柔性自除冰膜設(shè)計(jì)及研究
- 基于多源數(shù)據(jù)融合與機(jī)器學(xué)習(xí)的中老年女性骨質(zhì)疏松分層篩查模型研究
- 近藤晶格CeNiAsO中壓力誘導(dǎo)的量子臨界點(diǎn)的核磁共振研究
- 單取代的羧酸聯(lián)吡啶鎓鹽配體構(gòu)筑的多酸基金屬-有機(jī)配合物的合成及光催化性能研究
- 2025至2030中國(guó)卡介苗銷售行業(yè)發(fā)展趨勢(shì)分析與未來(lái)投資戰(zhàn)略咨詢研究報(bào)告
- 2025至2030中國(guó)單克隆抗體行業(yè)運(yùn)營(yíng)動(dòng)態(tài)與未來(lái)發(fā)展行情監(jiān)測(cè)報(bào)告
- 民法典案例解讀PPT
- 安全生產(chǎn)知識(shí)應(yīng)知應(yīng)會(huì)
- 質(zhì) 量 管 理 體 系 認(rèn) 證審核報(bào)告(模板)
- 腫瘤科新護(hù)士入科培訓(xùn)和護(hù)理常規(guī)
- 體育器材采購(gòu)設(shè)備清單
- 第4章 頜位(雙語(yǔ))
- 二手車鑒定評(píng)估報(bào)告書最終
- 電影場(chǎng)記表(雙機(jī)位)
- 塔吊負(fù)荷試驗(yàn)方案
- 電子商務(wù)專業(yè)“產(chǎn)教融合、五雙并行”人才培養(yǎng) 模式的實(shí)踐研究課題論文開(kāi)題結(jié)題中期研究報(bào)告(經(jīng)驗(yàn)交流)
- 購(gòu)買社區(qū)基本公共養(yǎng)老、青少年活動(dòng)服務(wù)實(shí)施方案
評(píng)論
0/150
提交評(píng)論