欢迎来到天天文库
浏览记录
ID:33581663
大小:194.29 KB
页数:42页
时间:2019-02-27
《java(第3章java基础程序设计)2011》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java语言程序设计JavaJava语言程序设计语言程序设计杨巍yangwei@mail.neu.edu.cnJava语言程序设计JavaJava编程基础编程基础——语句ControllingExecution杨巍yangwei@mail.neu.edu.cnJava编程基础Java语句¢顺序语句:按照从上至下的顺序一次执行¢条件语句:根据不同条件,执行不同的语句¢if¢if..else..¢if..elseif..¢if..elseif..elseif…else¢switch¢循环语句:重复执行某些动作¢for¢whilie¢do…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编程基础练习¢判断一个数字是奇数还是偶数¢如果一个数字除以2余数为0,则肯定是
3、偶数,相反一定是奇数5//IfElseDemo.javaJava编程基础importjava.util.Scanner;publicclassIfElseDemo{publicstaticvoidmain(String[]args){System.out.println("Pleaseinputthenumber:");Scannersc=newScanner(System.in);intx=sc.nextInt();if(x%2==0){System.out.println(x+"是偶数");}else{System.out.println(x+"是奇
4、数");}}}6Java编程基础练习¢求出两个数字中的最大值//MaxDemo.javapublicclassMaxDemo{publicstaticvoidmain(String[]args){intmax=0;intx=78;inty=89;…….System.out.println("最大值为:"+max);}}7//MaxDemo.javapublicclassMaxDemo{publicstaticvoidmain(String[]args){intmax=0;intx=78;inty=89;if(x>y){max=x;}else{max=y;
5、}System.out.println("最大值为:"+max);}}8Java编程基础ifStatements¢三目运算符Ternaryif-elseoperatorboolean-exp?表达式1:表达式2if(Boolean-expression){statement1;}else{statement2;}9//MaxDemo.javapublicclassMaxDemo{publicstaticvoidmain(String[]args){intmax=0;intx=78;inty=89;if(x>y){max=x;max=x>y?x:y}els
6、e{max=y;}System.out.println("最大值为:"+max);}}10Java编程基础swtichStatements¢swtichStatements¢多路选择语句selectionstatement¢用于处理从多个条件中选择一个合适的或是求多个结果¢Switch语句中选择值只能是字符或常量¢eachcaseendswithabreak¢Butthebreakisoptional11Java编程基础swtichStatementsswtich(integral-selector){caseintegral-value1:statem
7、ents;break;caseintegral-value2:statements;break;……caseintegral-valueN:statements;break;default:statements;}12//SwitchDemo1.javapublicclassSwitchDemo1{Java编程基础publicstaticvoidmain(String[]args){intx=78,y=89;charoper='+';switch(oper){case'+':{System.out.println("x+y="+(x+y));break;
8、}case'-':{System.out.println("x-y="+(x-y
此文档下载收益归作者所有