欢迎来到天天文库
浏览记录
ID:33556171
大小:169.88 KB
页数:30页
时间:2019-02-27
《java(第3章java基础程序设计for reading)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java语言程序设计Java语言程序设计杨巍yangwei@mail.neu.edu.cnJava语言程序设计Java编程基础——语句ControllingExecution杨巍yangwei@mail.neu.edu.cnJava编程基础Java语句顺序语句:按照从上至下的顺序一次执行条件语句:根据不同条件,执行不同的语句ifif..else..if..elseif..if..elseif..elseif…elseswitch循环语句:重复执行某些动作forwhiliedo…while3Java
2、编程基础ifStatementsif(Boolean-expression){if(Boolean-expression){statement;statements1;}}elseif(Boolean-expression){statements2;if(Boolean-expression){}elseif(Boolean-expression){statement1;statements3;}else{}else{statement2;statements4;}}4Java编程基础练习判断一个数字是奇数还是偶数
3、如果一个数字除以2余数为0,则肯定是偶数,相反一定是奇数IfElseDemo.java5Java编程基础练习求出两个数字中的最大值MaxDemo.java6Java编程基础ifStatements三目运算符Ternaryif-elseoperatorboolean-exp?表达式1:表达式2if(Boolean-expression){statement1;}else{statement2;}7Java编程基础swtichStatementsswtichStatements多路选择语句selectionstat
4、ement用于处理从多个条件中选择一个合适的或是求多个结果Switch语句中选择值只能是字符或常量eachcaseendswithabreakButthebreakisoptional8Java编程基础swtichStatementsswtich(integral-selector){SwitchDemo1.javacaseintegral-value1:statements;break;SwitchDemo2.javacaseintegral-value2:statements;break;……caseint
5、egral-valueN:statements;break;default:statements;}9Java编程基础whileStatements事先不知道循环该执行多少次While语句先判断,后执行while(Boolean-expression){statements;increment/decrementcounter;}10while(Boolean-expression){statements;increment/decrementcounter;}循环条件初值F判断条件Tstatementorbloc
6、k修改循环条件Java编程基础练习编写一个程序,实现1~100的累加通过While语句循环将100个数遍历一次,再进行求和运算WhileDemo.java12Java编程基础do…whileStatementsdo…whileThesoledifferencebetweenwhileanddo-whileisthatthestatementofthedo-whilealwaysexecutesatleastonce,eveniftheexpressionevaluatestofalsethefirsttimed
7、o{statements;increment/decrementcounter;}while(Boolean-expression);13do{statements;increment/decrementcounter;}while(Boolean-expression);循环条件初值statementorblock修改循环条件T判断条件FDoWhileDemo.javaJava编程基础forStatementsfor(Expression1;Expression2;Expression3){statements;}f
8、or(initialize;Boolean-expression;increment/decrement){statements;}Expression1;while(Expression2){Block;Expression3};15Java编程基础forStatementsForDemo1.java循环条件初值ForDemo2
此文档下载收益归作者所有