Java语言基础选择结构_第1页
Java语言基础选择结构_第2页
Java语言基础选择结构_第3页
Java语言基础选择结构_第4页
Java语言基础选择结构_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

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

文档简介

1、第二章 Java 语言基础(选择结构)新课引入public class Test1 public static void main(String args ) int x,y,z; x=1; y=2; z=x+y; System.out.println( x+y= +z); import java.io.*;public class Test2 public static void main(String args ) throws IOException int x,y,z; String s; BufferedReader r=new BufferedReader( new InputSt

2、reamReader(System.in); System.out.println(”enter x:”); s=r.readLine(); x=Integer.parseInt(s); /将该字符串转换为整型 System.out.println(”enter y:”); s=r.readLine(); y=Integer.parseInt(s); /将该字符串转换为整型 z=x+y; System.out.println( x+y= +z); 一、结构化编程流程控制语句用于控制程序中各语句的执行顺序。Java提供的流程控制语句有顺序结构、选择结构、循环结构、转移等。if(条件表达式) 语句

3、块1else 语句块2 if-else 语句流程图条件语句块1truefalse语句块2二、if 条件语句说明:if后的表达式是判定条件。一般为逻辑表达式或关系表达式。语句中if和else属于同一个条件语句,else子句不能作为语句单独使用,必须与if配对使用。语句序列可以含一条或多条执行语句。当有多条执行语句时必须用“ ”,将几个语句括起来成为一条复合语句,否则,只执行第一条语句。else及后面的语句序列可以省略。在if子句末不能加分号“ ;”if与else的配对原则二、if 条件语句例如:if(ab) System.out.println(”ab”);二、if 条件语句例如:if(scor

4、e=60) System.out.println(”你及格了”);else System.out.println(”你没有及格”);二、if 条件语句例1:判断一个数是否为奇数,如果为奇数则输出,否则不予理睬。public class IsOdd public static void main(String args) int in =11, mod;mod = in%2;if(mod=1) System.out.println(数字 + in + 为奇数);二、if 条件语句例2:要求在例1的执行过程中,如果用户输入偶数必须给出提示。二、if 条件语句if ( 条件表达式 )语句块1;els

5、e语句块2;import java.io.*;public class IsOdd2 public static void main(String args ) throws IOException int x; String s; BufferedReader r=new BufferedReader (new InputStreamReader(System.in); System.out.println(”enter an integer:”); s=r.readLine(); x=Integer.parseInt(s); if(x%2=1) System.out.println(你输入

6、的数字 + x+ 为奇数); else System.out.println(你输入的数字 + x + 为偶数); 例3:import java.io.*;public class Test public static void main(String args) throws IOException char ch; System.out.print(请输入一个字符:); ch = (char)System.in.read( ); / 从键盘输入一个字符 if (ch=0 & ch=85 & score=60 & score=0 & score60) System.out.println(不

7、通过); else System.out.println(成绩有误);二、ifelse ifelse多选择语句if(表达式1)if(表达式2)语句序列1else语句序列2elseif(表达式3)语句序列3else语句序列4二、ifelse ifelse多选择语句例如:if(Score60) System.out.println(不及格);else if(Score80) System.out.println(及格); else if(Score90)System.out.println(良好); else System.out.println(优秀);二、ifelse ifelse多选择语句例

8、4:根据年份和月份输出每月的天数。二、ifelse ifelse多选择语句public class IfTest public static int dayOfMonth(int year,int month) if(month=2) if(year%4=0&(year%400=0|year%100!=0) return 29; else return 28; else if(month=4|month=6|month=9|month=11) return 30; else return 31; public static void main(String args) int days=day

9、OfMonth(2008,2); System.out.println(days of February,2008 is: +days); 二、ifelse ifelse多选择语句三、switch 语句switch(表达式) case 值1:语句块 1 ;case 值2:语句块2 ;default:语句块n;switch 的常量和表达式可以是整型、字符型及byte型任何两个case常量值不可以有相同的值。只能对等式进行测试(即表达式的值是否等于值1、值2.,根据表达式取值的不同转向不同的分支。每个case分支中的语句块无须用花括号括起来。每个case分支都只是入口点可在case语句块中加入br

10、eak 语句,可立即转出switch语句(不再走后面case流程)。三、switch 语句例5:import java.io.*;public class Test public static void main(String args) throws IOException char ch ; System.out.print(请输入成绩(字符):); ch = (char)System.in.read() ; / 从键盘输入一个字符 switch (ch) case A : System.out.println(85100); break ; case B : System.out.pri

11、ntln(6084); break ; case C : System.out.println(059); break ; default : System.out.println(输入有误); 例6:import java.io.*;public class Test public static void main(String args) throws IOException char ch ; System.out.print(请输入成绩(字符):); ch = (char)System.in.read() ; / 从键盘输入一个字符 switch (ch) case a : case

12、A : System.out.println(85100); break ; case b : case B : System.out.println(6084); break ; case c : case C : System.out.println(059); break ; default : System.out.println(输入有误); 练习练习1:若a、b、c1、c2、x、y均是整型变量,正确的switch语句是 。(A)swich(a+b); (B)switch(a*a+b*b) case 1:y=a+b;break; case 3: case 0:y=a-b;break;

13、 case 1:y=a+b;break; case 3:y=b-a;break; (C)switch a (D)switch(a-b) case 3: case c1:y=a-b;break; case 4:x=a+b;break; case c2:x=a*d;break; case 10: default:x=a+b; case 11:y=a-b;break; default:y=a*b;break; 练习2:分析程序运行结果。public class s1 public static void main(String args) int x=1; switch(x) case 0: System.out.println(first);break; case 1: System.out.println(second); case 2: System.out.println(third); break; 练习3:分析程序运行结果

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论