




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Statements//commentcontentsinglelinecomment/*...commentcontent....*/multi-linecomments/**...commentcontent....*/textcommentsStatements
compound
statementscontrolstatementsmethodcallstatementsexpressionstatements2134canuse{}toenclosesomestatementstoformcompoundstatementsSystem.out.println(“Hello”);controlstatementsincludeconditionalbranchstatements,loopstatementsandjumpstatementsaddthesemicolonaftertheexpressionExecutablestatementInputStatementsInputandoutputaremainlyintheJava.iopackage,learnlaterStandardinputandoutputintheSystemClass,encapsulatedinJava.langSystemClasshas3constantsfortheinputandoutputofthesysteminStandardinputstream,whichmainlyprocessesdatainputfromkeyboardorotherinputdevicesout
Standardoutputstream,whichmainlyoutputsdatatothedisplayorotheroutputterminalserr
Standardoutputstream,mainlyoutputerrorinformationOutputdatatotheconsole:System.out.println();//wrapSystem.out.print();
//notwrapInputdatafromthekeyboard:ScannerClassScannerin=newScanner(System.in);Commonmethods:StringnextLine()Stringnext()intnextInt()doublenextDouble()booleannextBoolean()BasicProgramStructurecyclicstructureselectionstructuresequencestructure123refertotheexecutionofonebyoneintheorderinwhichthestatementsintheprogrambaseontheresultofconditionaljudgment,chooseoneoftwoormorepathstoexecuterepeatasetofoperationswithmultipletimesThreeBasicStructuresifStatementsProblem
Intheobject-orientedprogramming,whataretheclassicusesofif
statementsinJava?Writeaprogram,inputtwointegersfromthekeyboard,andoutputthelargervalueofthesetwonumbers.Howtoachievethis?
ifStatementsif…elseif…elseifUnbalancedif020103Unbalancedifif(expression){statements;}
Theexpressionisaconditionaljudgmentexpression,thestatementscanbethecompoundstatements,andtheentirestructureisastatement.expressionstatementstruefalseUnbalancedifforexample:Inputtwointegersfromthekeyboard,andoutputthelargervalueofthesetwonumbers.max<bmax=btruefalsemax=aif(max<b){max=b;}if…elseif(expression)statement1;elsestatement2;expressionstatement1truefalsestatement2if…elseforexample:Inputtwointegersfromthekeyboard,andoutputthelargervalueofthesetwonumbers.a>bmax=atruefalsemax=bif…elseifexpression1expression2expression3expression(n)statement(n+1)statement(n)statement3statement2statement1falsefalsefalsefalsetruetruetruetrueif(expression1)statement1;elseif(expression2)statement2;… … …elseif(expressionn)statement(n);elsestatement(n+1);Example:JudgewhethertheinputcharacterisanEnglishletter,number,spaceorother.letternumberspaceotherjudge
whetherthecharacteristheletterjudge
whetherthecharacteristhespacejudge
whetherthecharacteristhenumber||ch>=‘0’&&ch<=‘9’ch==''letterspacenumberif…elseifch>='a'&&ch<='z'ch>='A'&&ch<='Z'
Writeaprogram,inputthreeintegersfromthekeyboard,andoutputthemaximumvalueofthesethreenumbers.Howtoachievethis?PracticeSwitch
StatementsProblemInobject-orientedprogramming,howtousetheswitchstatementinJava?
Enterapercentilescore,andoutputgradesof'A’,'B’,'C',and'D'arerequired.Amongthem,90pointsormoreisA,70-89pointsisB,60-69pointsisC,and60pointsorlessisD.switch(expression){ caseconstantexpression1:<statement1>;<break;> caseconstantexpression2:<statement2>;<break;> … … … … caseconstantexpression(n):<statement(n)>;<break;> <default:statement(n+1)>;}
Thepartenclosedinthepairofanglebracketsisoptional.explanationSwitchString,int,enumExampleForexample:Enterapercentilescore,andoutputgradesof'A’,'B’,'C',and'D'arerequired.Amongthem,90pointsormoreisA,70-89pointsisB,60-69pointsisC,and60pointsorlessisD.a10090-9970-8960-6960orlessAnalysis:a/101097、866orless【Practice】Inputanynumberbetween1-7,theprogramwilloutputthecorrespondingdayoftheweekinEnglishaccordingtotheuser'sinput.Forexample,input3andtheprogramwilloutputWednesday.Ifyououtputanumberotherthan1-7,itwillpromptaninputerror!PracticeforCyclicStatementsProblemInobject-orientedprogramming,howtousetheforstatementinJava?Writeaprogramtofind1+2+3+……+100.forStatements
for(expression1;expression2;expression3)
loopbody;(1)solvingexpression1(2)judgmentloopconditionexpression2
Ifthevalueofexpression1istrue,executethestatementintheloopbody,andthenexecutestep(3);Ifthevalueisfalse,thenjumpoutoftheloopandexecutestep(5).(3)solvingexpression3(4)jumpbacktostep(2)andrepeat(5)aftertheloopfinished,executethefollowingstatementoftheloopbodycalculateexpression1judgeexpression2loopbodycalculateexpression3truefalseforStatementsFormat
for(expression1;expression2;expression3)
loopbody;计算表达式1判断表达式2循环体计算表达式3truefalseloopbody:
repeatedoperationexpression1:初始化条件,表示最初的状态expression2:判断条件,表示在什么情况下结束循环expression3:控制条件,控制初始化条件的变化s=0+1Example:writeaprogram,ask1+2+3+……+100。Example+2+3+4+……+100Loopbody:operationsperformedrepeatedlyExpression1:Initializationcondition,whichrepresentstheinitialstate.Expression2:Judgmentcondition,indicatingtheendoftheloopunderwhatcircumstances.Expression3:Controlcondition,controlthechangeofinitializationcondition.s=s+is=0i=1i<=100i++Accumulator,theinitialvalueis0.PrecautionsforusingtheforstatementsThethreeexpressionsintheforstatementcanbeomitted,butthe";"cannotbeomitted.Ifexpression1isomitted,theloopvariableshouldbeassignedaninitialvaluebeforetheforstatement.Ifexpression2isomitted,theloopwillfallintoaninfiniteloop,andthedetectionandexitloopmechanismshouldbearrangedelsewhere.Ifexpression3isomitted,theworkthatmakesthelooptendtoendshouldbearrangedelsewhere.NoteExpression1;
for(;;){
loopbody;
//addtheexitloopmechanismhere
expression3;}
for(expression1;expression2;expression3)
loopbody;whileanddo-whileloopProblemInobject-orientedprogramming,thedifferencebetweenwhileanddowhilestatementsinJava?Writeaprogramtofind1+2+3+……+100.Grammaticalformatwhile(expression){statementsequence;}whilestatementSolvetheloopconditionalexpression,ifthevalueistrue,gotostep2;ifthevalueisfalse,thenjumpoutoftheloopandgotostep4.Executethestatementinsidetheloopbodyofthewhilestatement.Jumpbacktostep1andrepeatexecution.executethestatementfollowingthebodyofthewhileloop.DescriptionwhilestatementLoopbody:operationsperformedrepeatedlyInitializationcondition:indicatestheinitialstateJudgmentcondition:IndicatestheendoftheloopunderwhatcircumstancesControlconditions:controlchangesininitializationconditions
expression1;
while(expression2){
loopbody;
expression3;}
for(expression1;expression2;expression3)
loopbody;whilestatementIssuesneedingattentionwhenusingthewhilestatement:theloopbodyshouldbewrittenintheformofcompoundsentencesavoid"endlessloops"020103thewhileloopjudgesfirstandthenloopsGrammaticalformatdo{statementsequence;}while(expression);do…whilestatementExecutethesequenceofstatementsinsidethebodyofthestatementloop.Solvetheloopconditionexpression,ifthevalueistrue,repeatstep1;otherwise,jumpoutoftheloopandexecutestep3.Executethestatementfollowingthebodyofthewhileloop.Descriptionatleastoncedo{statementsequence;}while(expression);while(expression){statementsequence;}do…whilestatementJumpstatementbreakstatementGrammaticalformatbreak;Terminatetheexecutionoftheswitchstatementorloopstatement(jumpoutofthesetwostatements),andmovetothesubsequentstatementforexecution.Effection一:break实例continuestatementGrammaticalformatcontinue;Endthiscycle.Thatis,sk
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年方便面行业食品安全与质量监管合作协议
- 2025版还建房项目配套设施租赁服务合同
- 二零二五年度酒店餐饮业内部承包经营合同
- 2025年电脑维修保养服务及智能化维修解决方案合同
- 2025年度高速公路工程招投标代理服务居间协议
- 二零二五年度单休制宠物护理师劳动合同范本(宠物服务)
- 二零二五年度创新型中小企业贷款咨询专项服务协议书
- 二零二五年度财务人员保密协议及高管离职竞业禁止协议
- 2025版高新技术开发区土地租用合同协议
- 2025版个人消费贷款合同
- 水利水电工程项目建议书、可行性研究、初步设计三阶段报告编制规程
- 《西游记》中师徒四人形象的现代解读与意义
- 成人重症患者人工气道湿化护理专家共识
- TCESA1281-2023TCCSA458-2023制造企业质量管理能力评估规范
- 2025年小学语文毕业升学考试全真模拟卷(语文综合运用能力提升版)试卷
- 2025年房屋漏水检测报告和鉴定报告
- T-CSTM 00824-2023 承压设备残余应力测定 压入能量差法
- 医院应依法设立35个委员会或领导小组相关法律依据
- 吊装作业技术交底内容
- 登革热及手足口病的护理
- 学校综合实践协议书
评论
0/150
提交评论