2023学年完整公开课版javastatements_第1页
2023学年完整公开课版javastatements_第2页
2023学年完整公开课版javastatements_第3页
2023学年完整公开课版javastatements_第4页
2023学年完整公开课版javastatements_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

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

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论