Java程序基础_第5-10讲 第3章 结构化编程

合集下载

JAVA各章知识总结

JAVA各章知识总结

Java语言是面向对象技术成功应用的典范。

诞生于1955年的Java语言在短短的几年间便席卷全球,以20世纪末网络科技和网络经济所特有的令人瞠目结舌的速度迅速发展,是21世纪开发信息系统的主流技术。

由于Java语言所具有的简洁性、纯属面向对象等特征,也使得它非常适合于在大学中面向对象程序设计的教学。

本学期我们开设了《Java语言与面向对象程序设计》这门课,通过对Java 一段时间的自学,我想在此谈谈我自己摸索学习Java的心得体会。

学习Java切忌浮躁,要注意以下几点:1.欲速则不达,初学者请不要被新技术迷惑,先把基础学扎实。

2.要扎扎实实,一步一个脚印的逐步学习,不要想着一步登天。

给自己定一个学习流程,按照流程学习。

3.多查API,它是Java编程的基本方法,也是编程过程中所不断利用的资源。

Java的学习过程不仅仅是基本语法的学习,更多的是去学习和掌握它所提供的API类库。

对于所接触到的类,方法,都去仔细去阅读文档的说明,再用自己编写的实例去比较一下。

4.看再多的书是学不全脚本的,要多实践,学习脚本最好的方法之一就是多练习;不要只看不练,一定要把书上的例子亲手敲到电脑上实践,千万不要照抄,更不要复制粘贴;看得懂代码,不代表会写代码。

学编程语言不仅仅是从理论上的学习,更重要的是要利用这门语言为你的思想服务。

理解这门语言是首要的,但是要达到心领神会、融会贯通就必须勤动手,多去时间,多编一些例子。

计算机科学是注重实践的学科,成功的软件开发人员无不经过大量的上机锻炼,只有理论和实践相结合才能真正掌握只是和技能。

5.看得懂的书,请仔细看;看不懂的书,请硬着头皮看;书读百遍,其义自现。

第一遍书看不懂就看第二遍、第三遍。

6.当你用脚本到一半却发现自己用的方法很拙劣时,请不要马上停手;请尽快将余下的部分粗略的完成以保证这个代码的完整性,然后分析自己的错误并重新编写和工作。

7.遇到问题,先自己想办法解决,查查文档,不要什么都不看就发帖子问。

Java编程基础及应用第3章运算符、表达式和语句

Java编程基础及应用第3章运算符、表达式和语句

(1)左移位运算符
左移位运算符的符号为“<<”,是二元运算符。左移位运算符左
面的操作元称为被移位数,右面的操作数称为移位量。

例如:a<<n运算的结果是通过将a的所有位都左移n位,每左移一 个位,左边的高阶位上的0或1被移除丢弃,并用0填充右面的低位 。
3.1.5 移位运算符

(2)右移位运算符
3.1.7 条件运算符

条件运算符是一个多元运算符,它的符号是“? : ”,需要连接三
个操作数,用法如下:a?b:c,要求a的值必须为boolean型数据 。运算法则是:当a的值为true时,a?b:c运算的结果是b的值;
当a的值是false时,a?b:c运算的结果是c的值。

例如: 8>2?10:20的结果是10;
// DevideTest2.java public class DevideTest2 { public static void main(String[] args) { int a = 13, b = 4, k; float f1, f2; k = a / b;//整数相除,结果仍为整数 f1 = a / b;//将整数结果强制转换为浮点型 f2 = (float) a / b;//将a强制转换为浮点 型,结果则为浮点型

(4)“按位异或”运算符
“^”是双目运算符。对两个整型数据a、b按位进行异或运算,运 算结果是一个整型数据c。运算法则是:如果a、b两个数据对应位
相同,则c的该位是0,否则是1。

例如:1001001和0101001进行“异或”运算,表达如下, 1001001 ^0101001 1100000

按照运算类型,可以将运算符分成以下几类:算术运算符,关系

结构化编程的三种基本结构

结构化编程的三种基本结构

结构化编程的三种基本结构
结构化编程是一种程序设计风格,强调程序的结构清晰、可读性强、易于维护。

它采用三种基本结构,分别是顺序结构、选择结构和循环结构。

顺序结构是程序中最基本的结构,它表示指令的执行顺序是从上往下依次执行。

在顺序结构中,每一条指令都必须按照特定的顺序依次执行,否则程序会出现错误。

选择结构是根据条件判断来选择执行哪一段程序。

常见的选择结构有if语句和switch语句。

if语句根据条件判断执行哪一段程序,而switch语句根据变量值执行哪一段程序。

循环结构是在满足一定条件下重复执行一段程序。

循环结构可以使程序更加灵活,让程序能够处理大量的数据。

常见的循环结构有
for循环、while循环和do-while循环。

for循环适合用于已知循环次数的情况,while循环适合用于不确定循环次数的情况,而
do-while循环则保证循环至少执行一次。

结构化编程的三种基本结构为程序员提供了一种清晰、可读性强、易于维护的程序设计方法。

程序员可以根据不同的需求和情况选择适当的结构来编写程序。

- 1 -。

Java开发技术大全第三章

Java开发技术大全第三章

第3章对象和类在当今的计算机大型应用软件开发领域,面向对象技术正在逐步取代面向过程的程序设计技术。

本章将介绍面向对象的基本知识和Java实现面向对象程序设计的主要工具--类。

如果读者缺乏关于面向对象程序设计的背景,一定要仔细地阅读本章。

如果读者有C++编程经验,也要注意二者之间的区别,毕竟Java在类的具体实现上与C++有较大的差别。

3.1 面向对象的基本概念面向对象(Object Oriented,OO)是当前计算机界关心的重点,它是90年代软件开发方法的主流。

面向对象的概念和应用已超越了程序设计和软件开发,扩展到很广的范围。

例如,数据库系统、交互式界面、应用结构、应用平台、分布式系统、网络管理结构、CAD技术、人工智能等领域。

谈到面向对象,这方面的文章非常多。

但是,明确地给出"面向对象"的定义却非常少。

最初,"面向对象"是专指在程序设计中采用封装、继承、抽象等设计方法。

可是,这个定义显然不能再适合现在的情况。

面向对象的思想已经涉及到软件开发的各个方面。

例如,面向对象的分析(Object Oriented Analysis,OOA),面向对象的设计(Object Oriented Design,OOD)以及经常说的面向对象的编程(Object Oriented Programming,OOP)。

许多有关面向对象的文章,都只是讲述在面向对象的开发中所需要注意的问题,或所采用的比较好的设计方法。

看这些文章只有真正懂得什么是对象,什么是面向对象,才能最大程度地收获知识。

说明:在本章中,着重讨论OOP,有关OOA和OOD请读者查阅有关软件工程的书籍。

OOP从所处理的数据入手,以数据为中心而不是以服务(功能)为中心来描述系统。

它把编程问题视为一个数据集合,因为数据相对于功能而言,具有更强的稳定性。

OOP同结构化程序设计相比最大的区别就在于:前者首先关心的是所要处理的数据,而后者首先关心的是功能。

Java程序设计教程 第2版 第3章 控制语句

Java程序设计教程 第2版 第3章 控制语句

3.3.1 if语句
1、 简单的if语句 简单的if语句的语法形式:
if(条件表图。
条件表达式

真 子句
【例3.2】 将三个整数按由小到大的顺序排列并输出。Example3_2.java
3.3.1 if语句
2、 if-else语句 if-else语句的语法形式:
第3 章
控制语句
Java程序设计
导读 Introduction
一个Java程序由一个或多个类组成,即类是组成Java程序的基 本单位。而每一个类由域(field)和方法(method)组成,而方法 是由若干条语句组成的。所以,编写Java程序必须先从语句开始。
本章中将要详细讲解Java语言中的控制语句。
语句n+1
3.3.1 if语句
4、衍生的if-else if语句 if-else if语句的执行过程如图。
条件表达式1
真 语句1
假 条件表达式2 真 语句2
...

条件表达式n 真
语句n
假 语句n+1
【例3.6】改写【例3.1】,使程序能够求解出任何一个二元一次方程的根。 Example3_6.java
3.3.2 switch语句
switch语句的语法形式:
switch(表达式) {
case 常量1:语句块1;break; case 常量2:语句块2;break; … case 常量n:语句块n;break; default:语句块n+1; }
3.3.2 switch语句 switch语句的执行过程。
【例3.5】 改写例【例3.4】的程序,形成if-else if语句形式。 Example3_5.java

java基础教案(含实验内容)

java基础教案(含实验内容)

课程名称:java程序设计时间:2015-2016学年第二学期授课教师:****授课班级:151软件(30人)、151计网(25人)Java程序设计:第一章:Java语言概述2课时第二章:java语言基础4课时第三章流程控制10课时第四章数组4课时第五章字符串2课时第六章类及对象4课时第七章java语言类的特性12课时第八章继承、抽象类和接口22课时复习、机动4课时合计:64课时推荐用书(图):1、Java程序设计基础(第4版)实验指导邹林达陈国君主编清华大学出版社ISBN 978—7—302—35279—22、Java程序设计基础(第4版)陈国君主编清华大学出版ISBN 978—7—302-33142-1Java程序设计授课计划:第1周第一章:Java语言概述2课时2课时第1周第二章:java语言基础—数据类型2课时第2周第二章:java语言基础—运算符及表达式2课时4课时第2周第三章流程控制—选择结构2课时第3周第三章流程控制-选择结构之多分支2课时第3周第三章流程控制-while循环嵌套2课时第4周第三章流程控制-for循环嵌套2课时第4周第三章流程控制-循环嵌套2课时10课时第5周第四章数组-一维数组2课时第一章:Java语言概述(2课时)教学目标:●课程所讲述的内容●Java语言的由来●Java语言的版本说明●Java语言的跨平台●Java开发环境配置教学重难点:●配置开发环境➢什么是Java语言:java语言的发展及开发环境➢Java语言的基本结构:java语言的基本结构➢Java语言的基本要素:java基本语法/类及对象/继承及接口✓Java语言高级知识:➢异常、IO、多线程➢Java异常处理及捕获机制➢Java输入及输出流➢Java多线程➢Java图形用户组建Swing类2.计算机语言发展的四个阶段1)机器语言2)汇编语言3)高级语言4)结构化程序设计语言3.java发展背景1)sun公司在很多领域有前瞻性,提前预判到一些“科技”及人类生活发展的要素。

JAVA自学教程(完整版)PPT课件(2024)

JAVA自学教程(完整版)PPT课件(2024)

二分查找
针对有序数组,每次取中间元 素与目标元素比较,缩小查找 范围
12
03 面向对象编程基础
2024/1/27
13
类与对象的概念
类的定义
类是对象的模板,它定 义了对象的属性和方法 。
2024/1/27
对象的概念
对象是类的实例,具有 类定义的属性和行为。
类与对象的关系
类是对象的抽象描述, 而对象是类的具体实现 。
2024/1/27
32
Socket通信原理及示例
Socket通信原理
理解Socket通信的基本原理,掌握Socket 类和ServerSocket类的使用。
TCP编程
学习基于TCP协议的Socket通信,实现客户 端与服务器之间的数据传输。
多线程处理
掌握多线程在Socket通信中的应用,提高服 务器的并发处理能力。
TreeSet类的特点和使用
TreeSet是Set接口的另一个常用实现类,它基于红黑树实 现。TreeSet会对元素进行排序,因此它适用于需要排序的 场景。
26
Map接口及其实现类
01
Map接口的定义和特 点
Map接口表示一种键值对的映射关系 。Map中的每个元素都包含一个键和 一个值,键在Map中是唯一的。
学习ReentrantLock锁的使用,了解 公平锁与非公平锁的区别。
2024/1/27
等待/通知机制
掌握Object类的wait()、notify()和 notifyAll()方法的使用,实现线程间 的通信。
死锁与避免
了解死锁的概念及产生条件,学习如 何避免死锁的发生。
31
网络编程基础
网络编程概述
ArrayList类的特点和使用

第3章-面向对象程序设计(上)ppt课件(全)

第3章-面向对象程序设计(上)ppt课件(全)

面向对象的基本概念
❖ 对象(Object)是一个应用系统中用来描述客观 事物的实体,是具有特定属性(数据)和行为 (方法)的基本运行单位,是类的一个特定状态 下的实例。
❖ 类(Class)是Java代码的基本组织模块,是用 以描述一组具有共同属性和行为的对象的基 本原型,是对这组对象的概括、归纳与抽象 表达,它定义了本类对象所应拥有的状态属 性集及操作这组属性的行为方法集。
也可以一步完成,即:
类名称 对象变量 = new 类名称();
实例化对象(续)
❖ 一旦一个类的实例化对象产生,就可以通过该对象 访问类中定义的成员了。通过对象访问成员的基本 结构如下:
对象变量.属性 = 值; 对象变量.方法();
3.2 方法
任务2--Leabharlann 方法的定义【任务内容】给任务1中的媒体播放器类增加控制方 法
❖ JAVA的类由类的声明与类体两部分组成。
1、类的声明用来指定该类的访问修饰符、类的
名称、父类名称及实现接口等属性。声明类的完整 形式为: [public][abstract|final] class <类名> [extends<基类名> ]
[implements <接口列表>]
类名应该能够描述出类的 特征或作用,类名的第一 个字母通常要大写,如果 类名由多个单词组成,则 每一单词的首字母一般都
【范例 3-1】 设计一个媒体播放器类
class MediaPlayer{ //音乐文件路径 public String musicFile; //播放器音量,范围:0(静音)~100 public int soundLevel; //播放器状态 public boolean isPlaying;

结构化编程教案

结构化编程教案

结构化编程教案教案标题:结构化编程教案教学目标:1. 了解结构化编程的概念和原则;2. 掌握结构化编程的基本概念和技巧;3. 能够运用结构化编程的思维方式设计和编写简单的程序;4. 培养学生的逻辑思维和问题解决能力。

教学准备:1. 计算机和投影仪;2. 结构化编程相关的教材和参考资料;3. 编程软件(如Python、C等);4. 练习题和实例。

教学过程:一、导入(5分钟)1. 向学生介绍结构化编程的概念,解释其重要性和应用领域;2. 引导学生思考结构化编程与其他编程方法的区别和优势。

二、理论讲解(15分钟)1. 解释结构化编程的基本原则,如顺序、选择和循环结构;2. 介绍结构化编程的基本概念,如顺序结构、分支结构和循环结构;3. 给出实例,解释如何使用结构化编程思维解决问题。

三、示范演示(20分钟)1. 通过编程软件展示结构化编程的实际应用;2. 按照结构化编程的原则,演示如何设计和编写简单的程序;3. 解释每个步骤的目的和意义,引导学生理解结构化编程的思维方式。

四、练习与实践(30分钟)1. 分发练习题,让学生独立或小组完成编程任务;2. 引导学生运用结构化编程的思维方式解决问题;3. 鼓励学生在实践中发现问题并尝试解决,培养问题解决能力。

五、讨论与总结(10分钟)1. 让学生分享他们的编程经验和遇到的问题;2. 引导学生总结结构化编程的优势和局限性;3. 结合实例,再次强调结构化编程的重要性和应用价值。

六、作业布置(5分钟)1. 布置结构化编程的相关作业,要求学生进一步巩固所学知识;2. 提供参考资料和练习题,鼓励学生主动学习和探索。

教学评估:1. 在练习与实践环节观察学生的编程过程和结果;2. 针对学生的作业和讨论表现进行评价;3. 分析学生对结构化编程概念和技巧的掌握程度。

教学延伸:1. 鼓励学生参与编程竞赛或项目实践,进一步提升结构化编程能力;2. 推荐相关的编程书籍和网上资源,供学生深入学习和研究。

Java语言程序设计课后习题解答-张思民-第三章

Java语言程序设计课后习题解答-张思民-第三章

第3章面向对象程序设计基础【1】什么是Java程序使用的类?什么是类库?[解答]:Java程序的基本单位是类。

对象是对事物的抽象,而类是对对象的抽象和归纳,找出事物的共性,把具有共同性质的事物归结为一类,得出一个抽象的概念——类。

类是具有相同属性和方法的一组对象的集合,类是在对象之上的抽象,对象则是类的具体化,一旦建立类之后,就可用它来建立许多你需要的对象。

Java的类库是系统提供的已实现的标准类的集合,是Java编程的API(Application Program Interface),它可以帮助开发者方便、快捷地开发Java程序。

【2】如何定义方法?在面向对象程序设计中方法有什么作用?[解答]:方法的定义由两部分组成:方法声明和方法体。

方法声明的基本格式如下:返回类型方法名(形式参数){… //方法体内容}方法声明包括方法名、返回类型和形式参数,方法的小括号是方法的标志;方法体为实现方法行为的Java语句。

在面向对象程序设计中,方法所起的作用是完成对类和对象属性操作。

【3】简述构造方法的功能和特点。

下面的程序片段是某学生为student类编写的构造方法,请指出其中的错误。

void Student(int no,String name){studentNo=no;studentName=name;return no;}[解答]:构造方法是一个特殊的方法,主要用于初始化新创建的对象。

构造方法的方法名要求与类名相同,用户不能直接调用,只能通过new运算符调用,而且构造方法是不返回任何数据类型,甚至也不返回void数据类型,即不能在构造方法前加void。

以上的代码段出错于:①构造方法Student()前不能加void ②不能用return语句【4】定义一个表示学生的student类,包括的域有学号、姓名、性别、年龄,包括的方法有获得学号、姓名、性别、年龄及修改年龄。

编写Java程序创建student类的对象及测试其方法的功能。

JAVA自学教程(完整版)PPT课件

JAVA自学教程(完整版)PPT课件

而在Java语言中,Java自带的虚拟机很好地实现了跨平台 性。Java源程序代码经过编译后生成二进制的字节码是与平台 无关的,但是可被Java虚拟机识别的一种机器码指令。Java虚 拟机提供了一个字节码到底层硬件平台及操作系统的屏障,使 得Java语言具备跨平台性。
2. 面向对象
面向对象是指以对象为基本粒度,其下包含属性和方法。 对象的说明用属性表达,而通过使用方法来操作这个对象。面 向对象技术使得应用程序的开发变得简单易用,节省代码。 Java是一种面向对象的语言,也继承了面向对象的诸多好处, 如代码扩展、代码复用等。
图1.8 Applet显示“Hello World!”
习题
1. 简述Java的特点。 2. 简述Java的分类情况。 3. 进行Java环境的安装和配置。 4. 编写应用程序,屏幕上输出“欢迎来到Java世界!”。 5. 编写Applet,输出“欢迎来到Java世界!”。
第2章 Java基本语法
1.4 JDK包的下载与安装
Java Develop Kit简称为JDK,是Sun公司免费发行的软件包, 可以从Sun网站免费下载,也可以从其它国内 地址下载。JDK版本从1.02开始,目前版本发展到1.4,其中高 级版本对低级版本实现向下兼容。运用这个软件包,就可以对 Java源程序进行编译和运行。本书中下载使用的JDK包为j2sdk1_4_0_012-windows-i586.exe。 下载后双击图标,即可进行安装, 默认的安装目录为C:\j2sdk1.4.0_01。本书作者将安装目录改为 D:\j2sdk1.4.0_01。
图1.4 编辑lib变量
选中path变量,单击“编辑”按钮,弹出标题为“编辑用 户变量”的对话框,如图1.5所示。在变量值一栏的最后添加 “; D:\j2sdk1.4.0_01\bin”,“;”表示与前面的各项隔开,后面的 路径是JDK包的安装路径下的bin目录。图1.5为作者修改path变 量的情况,注意你的安装路径可能与作者的有所不同,同样要 以你的安装路径为基准进行修改。

东北大学《计算机基础》课件-第5章(张老师)

东北大学《计算机基础》课件-第5章(张老师)
2. C++与C语言的关系
C++保持了C语言的简洁、高效和接近汇编语言等优点,同时又对C语言的 不足和问题作了很多重要改进。
①增加了新的运算符,使C++应用起来更加方便;②改进了类型系统,增 加了安全性;③使用“引用”作函数参数为用户编程带来了很大方便;④允

函数重载,允许设置缺省参数,提高了编程的灵活性,减少了冗余返;回⑤本对章目
1. C++的特点
1抽象:是对具有特定属性及行为特征的对象进行概括,从中 提炼 出这一类对象的共性,并从通用性的角度描述其共有的属性及 行 为特征。抽象又分为数据抽象和代码抽象,前者描述某类对象的 公共属性,后者描述某类对象共有的行为特征。 2封装和数据隐藏:在面向对象程序设计中,通过封装可以将 一部分 属性和数据的操作隐藏起来,另一部分作为类的外部接口, 使用者 可以访问。这样可以对属性和操作的访问权限进行合理控制, 减少 程序之间的相互影响,降低出错的可能性。
3 确定数据结构和算法:数据结构 + 算法 = 程序
4 编写程序5 调试程序6 Fra bibliotek理资料,交付使用
返回本节目录
5.1.3 程序设计语言
程序设计语言是人们根据描述问题的需要而设计的,是计算机能 够直接识别的语言,有一套固定的符号和语法规则,是人与计算机交 流所使用的“语言”。
程序设计语言可分成机器语言、汇编语言和高级语言。前两类依 赖于计算机硬件,因机器而异,又称为低级语言,而高级语言与计算 机硬件基本无关,是目前使用非常广泛的程序设计语言。
程序设计语言就是计算机能够理解和执行的特殊语言。
5.1.2 程序设计的一般过程
概括地说,程序设计就是分析问题、编写程序、调试程序的过 程。用计算机解决实际问题的基本过程如下图所示。

《Java程序设计基础》第3章:流程控制

《Java程序设计基础》第3章:流程控制

控制循环
控制循环是在循环执行过程中改变循环条件或跳出循环的语句。
1 break语句
break语句用于跳出当前循 环或switch语句。
2 continue语句
3 return语句
continue语句用于跳过本次 循环中的剩余代码,进入下 一次循环。
return语句用于结束方法的 执行,并返回一个值。
分支结构
分支结构根据条件的真假选择执行பைடு நூலகம்同的代码块。
if语句
if语句根据条件是否成立来执行不同的语句块。
if-else-if语句
if-else-if语句可以根据多个条件判断选择执行不同 的代码块。
if-else语句
if-else语句根据条件的真假执行不同的代码块。
switch语句
switch语句根据某个表达式的值选择执行相应的代 码块。
方法的返回值
方法可以有返回值,用于将计算结果或特定的值 返回给调用者。
方法的参数传递
方法可以接受参数,用于传递数据给方法进行处 理。
数组的基本概念
数组是一种存储多个相同类型元素的数据结构。
声明数组
声明数组即定义数组的类型和名称。
初始化数组
初始化数组即为数组的元素赋初值。
数组的遍历
数组的遍历即对数组中的每个元素进行访问和操 作。
方法和数组
方法和数组是在流程控制中广泛使用的工具。
静态方法
静态方法是属于类的,可以直 接调用而不需要创建对象。
非静态方法
非静态方法是属于对象的,需 要通过对象来调用。
方法重载
方法重载是指在一个类中定义 多个同名方法,但参数类型或 个数不同。
递归方法
递归方法是指在方法内部调用自身的方法。

第3章结构化程序设计的三种基本结构

第3章结构化程序设计的三种基本结构

表3.4 printf函数常用格式说明符
格式 说明符
功能说明
d
表示输出带符号的十进制整数(正数不输出符号)
c
表示输出单个字符
s
表示输出多个字符,即一个字符串
f
表示以小数形式输出实数,默认输出6位小数
⑤修饰符作为附加格式说明符,在printf函数中常用的 修饰符如表3.6所示。
表3.5 printf函数常用修饰符
A
假 条件 真
流程图
A 直到条件为真
N-S 结构图
3.1顺序结构程序设计
3.1.1赋值语句 赋值是把一个运算表达式的值赋给变量。
1.格式 简单赋值是把一个运算表达式的值赋给一个变量,一
般的形式为: 变量=<运算表达式>
2.功能 赋值语句的功能是将运算表达式的值赋给左边的变量。
3.说明: (1)以上形式也被称为赋值表达式,“=” 是运算符,称为赋值号; (2)赋值号与数学中的等于号“=”完全不一样,并非相等的意思。 故可以有如下的赋值语句:
3.1.2标准输入/输出语句及使用
1.标准格式输入函数scanf ( ) (1)一般格式:
scanf ("<格式控制串>",参数表); 其中,函数的参数分为格式控制串和参数表两部分,中间 用逗号分隔 ①格式控制串部分是加上双引号的一个字符串,可以用一 般字符作为匹配符,也可以用格式说明符。 ②格式控制串由下列形式组成: % <修饰符><格式说明符> 其中,“%”是格式标识符,格式说明符用来表示输入的格 式,scanf函数常用的格式说明符如表3.2所示。
修饰符
功能说明
字母l
表示输入长整型整数,可加在格式说明符d、o、x 前面

第3章程序控制结构结构化程序的三种基本结构1、顺序结

第3章程序控制结构结构化程序的三种基本结构1、顺序结
第3章 程序控制结构 结构化程序的三种基本结构: 1、顺序结构(order structure) 2、选择结构(selection structure) 3、循环结构(repetition structure)
Bohm和Jacopini的研究证实,所有的程序都能够只用以上 三种控制结构编写。
3.1 C语言的语句
3.3 分支结构 3.3.1 if 语句 一、if语句的三种形式 1、if(表达式) 语句
假 表达式
真 语句
当表达式值为真(非0)时,执 行if中的语句,否则执行if后的 语句。
可以是复 合语句
例: include <stdio.h> main( ) {int x=10,y=20,z;
z=x; if (z>y)
N
a=b=0,c不为0 Y 无解
N
a=0,b不为0 Y x= -c/b
N
d=b*b-4*a*c
d>0
N
d=0
N
1
Y X1= (-b+sqrt(d))/(2*a) X2= (-b-sqrt(d))/(2*a)
Y X1=X2= -b/(2*a)
1 该方程无实根
END
scanf(“%d”,&x); if (x<0) y=-1;
错误!
ssccaannff((““%%dd””,,&&xx));; iiff((xx>>==00))
else if(x==0);
if(x>i0f)(xy>=01); y=1;
y=0;
else ye=ls0e; y=0;
else y=1;
eellsseeyy==--11;;

《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案

《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案

《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案3.1public class Exercise03_01 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a, b, c: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double discriminant = b * b - 4 * a * c;if (discriminant < 0) {System.out.println("The equation has no real roots");}else if (discriminant == 0) {double r1 = -b / (2 * a);System.out.println("The equation has one root " + r1);}else { // (discriminant > 0)double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);System.out.println("The equation has two roots " + r1 + " and " + r2);}}}3.1附加public class Exercise03_01Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter a numerator: ");int numerator = input.nextInt();System.out.print("Enter a denominator: ");int denominator = input.nextInt();if (numerator < denominator) {System.out.println(numerator + " / " + denominator + " is a proper fraction");}else if (numerator % denominator == 0) {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and it can be reduced to " + numerator / denominator);}else {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and its mixed fraction is " + numerator / denominator + " + " +numerator % denominator + " / " + denominator);}}}3.2public class Exercise03_02 {public static void main(String[] args) {Scanner input = new Scanner(System.in);int number1 = (int)(System.currentTimeMillis() % 10);int number2 = (int)(System.currentTimeMillis() * 7 % 10);int number3 = (int)(System.currentTimeMillis() * 3 % 10);System.out.print("What is " + number1 + " + " + number2 + " + " +number3 + "? ");int answer = input.nextInt();System.out.println(number1 + " + " + number2 + " + " + number3 +" = " + answer + " is " +(number1 + number2 + number3 == answer));}}3.2附加public class Exercise03_02Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter the coordinates for two points: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double m = (y2 - y1) / (x2 - x1);double b = y1 - m * x1;System.out.print("The line equation for two points (" + x1 + ", " + y1 + ") and (" + x2 + ", " + y2 + ") is " + "y = ");if (m == -1)System.out.print("-x");else if (m == 1)System.out.print("x");elseSystem.out.print(m + "x");if (b > 0)System.out.println(" + " + b);else if (b < 0)System.out.println(" - " + (-1 * b));else// b is 0System.out.println();}}3.3public class Exercise03_03 {public static void main(String[] args) {Scanner input = new Scanner(System.in); System.out.print("Enter a, b, c, d, e, f: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double d = input.nextDouble();double e = input.nextDouble();double f = input.nextDouble();double detA = a * d - b * c;if (detA == 0) {System.out.println("The equation has no solution"); }else {double x = (e * d - b * f) / detA;double y = (a * f- e * c) / detA;System.out.println("x is " + x + " and y is " + y);}}}3.3附加public class Exercise03_03Extra {public static void main(String[] args) {final double RADIUS = 5;double angle = Math.random() * 2 * Math.PI;double x = RADIUS * Math.random() * Math.cos(angle);double y = RADIUS * Math.sin(angle);double distance = Math.pow(x * x + y * y, 0.5);System.out.println("The point is (" + x + ", " + y + ") and its distance to the center is " + distance);}}3.4public class Exercise03_04 {public static void main(String[] args) {int number = (int)(Math.random() * 12) + 1;// or int number = (int)(System.currentTimeMillis() % 12 + 1);// or int number = (int)(Math.random() * 12) + 1;if (number == 1)System.out.println("Month is Januaray");else if (number == 2)System.out.println("Month is Feburary");else if (number == 3)System.out.println("Month is March");else if (number == 4)System.out.println("Month is April");else if (number == 5)System.out.println("Month is May");else if (number == 6)System.out.println("Month is June");else if (number == 7)System.out.println("Month is July");else if (number == 8)System.out.println("Month is August");else if (number == 9)System.out.println("Month is September");else if (number == 10)System.out.println("Month is October");else if (number == 11)System.out.println("Month is November");else// if (number == 12)System.out.println("Month is December");}}3.5public class Exercise03_05 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter an integer for todaySystem.out.print("Enter today抯 day: ");int today = input.nextInt();System.out.print("Enter the number of days elapsed since today: ");int elapsedDays = input.nextInt();String nameForToday;if (today == 0)nameForToday = "Sunday";else if (today == 1)nameForToday = "Monday";else if (today == 2)nameForToday = "Tuesday";else if (today == 3)nameForToday = "Wednesday";else if (today == 4)nameForToday = "Thursday";else if (today == 5)nameForToday = "Friday";else// if (today == 6)nameForToday = "Saturday";int futureDay = (today + elapsedDays) % 7; String nameForFutureDay;if (futureDay == 0)nameForFutureDay = "Sunday";else if (futureDay == 1) nameForFutureDay = "Monday";else if (futureDay == 2) nameForFutureDay = "Tuesday";else if (futureDay == 3) nameForFutureDay = "Wednesday";else if (futureDay == 4) nameForFutureDay = "Thursday";else if (futureDay == 5) nameForFutureDay = "Friday";else// if (futureDay == 6) nameForFutureDay = "Saturday";System.out.println("Today is " + nameForToday+ " and the future day is " + nameForFutureDay); } }3.6public class Exercise03_06 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter weight in pounds System.out.print("Enter weight in pounds: "); double weight = input.nextDouble();// Prompt the user to enter heightSystem.out.print("Enter feet: ");double feet = input.nextDouble();System.out.print("Enter inches: ");double inches = input.nextDouble();double height = feet * 12 + inches;// Compute BMIdouble bmi = weight * 0.45359237 / ((height * 0.0254) * (height * 0.0254));// Display resultSystem.out.println("BMI is " + bmi);if (bmi < 18.5)System.out.println("Underweight");else if (bmi < 25)System.out.println("Normal");else if (bmi < 30)System.out.println("Overweight");elseSystem.out.println("Obese");}}3.7/** Break down an amount into smaller units* Display the non-zero denominations only, and display singular* words for single units like 1 dollars, 1 penny, and display plural * words for more than one unit like 2 dollars, 3 pennies.*/public class Exercise03_07 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Receive the amount entered from the keyboardSystem.out.print("Enter an amount in double, for example 11.56: ");double amount = input.nextDouble();int remainingAmount = (int)(amount * 100);// Find the number of one dollarsint numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;// Find the number of dimes in the remaining amountint numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;// Find the number of nickels in the remaining amountint numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;// Find the number of pennies in the remaining amountint numberOfPennies = remainingAmount;// Display resultsif (amount < 0) {System.out.println("Your amount is negative");System.exit(1);}else if (amount < 0) {System.out.println("Your amount is zero");System.exit(2);}System.out.println("Your amount " + amount + " consists of ");if (numberOfOneDollars > 1)System.out.println(numberOfOneDollars + "\ dollars");else if (numberOfOneDollars == 1)System.out.println(numberOfOneDollars + "\ dollar");if (numberOfQuarters > 1)System.out.println(numberOfQuarters + "\ quarters");else if (numberOfQuarters == 1)System.out.println(numberOfQuarters + "\ quarter");if (numberOfDimes > 1)System.out.println(numberOfDimes + "\ dimes");else if (numberOfDimes == 1)System.out.println(numberOfDimes + "\ dime");if (numberOfNickels > 1)System.out.println(numberOfNickels + "\ nickels");else if (numberOfNickels == 1)System.out.println(numberOfNickels + "\ nickel");if (numberOfPennies > 1)System.out.println(numberOfPennies + "\ pennies");else if (numberOfPennies == 1)System.out.println(numberOfPennies + "\ penny");}}3.8public class Exercise03_08 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three numbersSystem.out.print("Enter three integers: ");int number1 = input.nextInt();int number2 = input.nextInt();int number3 = input.nextInt();if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}if (number2 > number3) {int temp = number2;number2 = number3;number3 = temp;}if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}System.out.println("The sorted numbers are "+ number1 + " " + number2 + " " + number3);}}public class Exercise03_09 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter an integerSystem.out.print("Enter the first 9 digits of an ISBN as integer: ");int number = input.nextInt();// Calculate checksum (You may write a loop to simplify it in Ch4int checksum =((number / 100000000 % 10) * 1 +(number / 10000000 % 10) * 2 +(number / 1000000 % 10) * 3 +(number / 100000 % 10) * 4 +(number / 10000 % 10) * 5 +(number / 1000 % 10) * 6 +(number / 100 % 10) * 7 +(number / 10 % 10) * 8 +(number % 10) * 9) % 11;System.out.print("The ISBN-10 number is ");// Display leading zeros, improve the solution using loops in the next chapterif (number / 100000000 == 0) {System.out.print("0");if (number / 10000000 == 0) {System.out.print("0");if (number / 1000000 == 0) {System.out.print("0");if (number / 100000 == 0) {System.out.print("0");if (number / 10000 == 0) { System.out.print("0");if (number / 1000 == 0) { System.out.print("0");if (number / 100 == 0) {System.out.print("0");if (number / 10 == 0) {System.out.print("0");if (number == 0) {System.out.print("0");}}}}}}}}}System.out.print(number);if (checksum == 10)System.out.print("X");elseSystem.out.print(checksum);}}3.10public class Exercise03_10 {public static void main(String[] args) {// 1. Generate two random single-digit integersint number1 = (int)(Math.random() * 10);int number2 = (int)(Math.random() * 10);// 2. Prompt the student to answer 搘hat is number1 + number2?? System.out.print("What is " + number1 + " + " + number2 + "? "); Scanner input = new Scanner(System.in);int answer = input.nextInt();// 4. Grade the answer and display the resultString replyString;if (number1 + number2 == answer)replyString = "You are correct!";elsereplyString = "Your answer is wrong.\" + number1 + " + "+ number2 + " should be " + (number1 + number2);System.out.println(replyString);}}3.11public class Exercise03_11 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter inputSystem.out.print("Enter a month in the year (e.g., 1 for Jan): ");int month = input.nextInt();System.out.print("Enter a year: ");int year = input.nextInt();int numberOfDaysInMonth = 0;switch (month) {case 1:System.out.print("January " + year);numberOfDaysInMonth = 31;break;case 2:System.out.print("February " + year);if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { numberOfDaysInMonth = 29;}else {numberOfDaysInMonth = 28;}break;case 3:System.out.print("March " + year);numberOfDaysInMonth = 31;break;case 4:System.out.print("April " + year);numberOfDaysInMonth = 30;break;case 5:System.out.print("May " + year);numberOfDaysInMonth = 31;break;case 6:System.out.print("June " + year);numberOfDaysInMonth = 30;break;case 7:System.out.print("July " + year);numberOfDaysInMonth = 31;break;case 8:System.out.print("August " + year);numberOfDaysInMonth = 31;break;case 9:System.out.print("September " + year);numberOfDaysInMonth = 30;break;case 10:System.out.print("October " + year);numberOfDaysInMonth = 31;break;case 11:System.out.print("November " + year);numberOfDaysInMonth = 30;break;case 12:System.out.print("December " + year);numberOfDaysInMonth = 31;break;}System.out.print(" has " + numberOfDaysInMonth + " days"); }}3.12public class Exercise03_12 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a three-digit integer: ");int number = input.nextInt();if (number / 100 == number % 10)System.out.println(number + " is a palindrome");elseSystem.out.println(number + " is not a palindrome");}}3.13public class Exercise03_13 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter filing statusSystem.out.print("(0-single filer, 1-married jointly or qualifying widow(er),"+ "\2-married separately, 3-head of household)\" + "Enter the filing status: ");int status = input.nextInt();// Prompt the user to enter taxable incomeSystem.out.print("Enter the taxable income: ");double income = input.nextDouble();// Compute taxdouble tax = 0;if (status == 0) { // Compute tax for single filersif (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 82250) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950)* 0.25; } else if (income <= 171550) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;} else if (income <= 372950) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;}} else if (status == 1) { // Compute tax for married file jointly if (income <= 16700) {tax = income * 0.10;} else if (income <= 67900) {tax = 16700 * 0.10 + (income - 16700) * 0.15;} else if (income <= 137050) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25; } else if (income <= 208850) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;} else if (income <= 372950) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;} else {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (171950 - 137050) * 0.28 + (372950 - 208850) * 0.33+ (income - 372950) * 0.35;}} else if (status == 2) { // Compute tax for married separately if (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 68525) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25; } else if (income <= 104425) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;} else if (income <= 186475) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;}} else if (status == 3) { // Compute tax for head of household if (income <= 11950) {tax = income * 0.10;} else if (income <= 45500) {tax = 11950 * 0.10 + (income - 11950) * 0.15;} else if (income <= 117450) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25; } else if (income <= 190200) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;} else if (income <= 372950) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;} else {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33+ (income - 372950) * 0.35;}} else {System.out.println("Error: Wrong filing status");System.exit(1);}// Display the resultSystem.out.println("Tax is " + (int) (tax * 100) / 100.0);}}3.14public class Exercise03_14 {public static void main(String[] args) {// Obtain the random number 0 or 1int number = (int)(Math.random() * 2);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Guess head or tail? " +"Enter 0 for head and 1 for tail: ");int guess = input.nextInt();// Check the guessif (guess == number)System.out.println("Correct guess");else if (number == 0)System.out.println("Sorry, it is a head");elseSystem.out.println("Sorry, it is a tail");}}3.15public class Exercise03_15 {public static void main(String[] args) {// Generate a lotteryint lottery = (int)(Math.random() * 1000);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter your lottery pick (three digits): ");int guess = input.nextInt();// Get digitsint l1 = lottery / 100;int l2 = (lottery % 100) / 10; // l2 = (lottery / 10) % 10int l3 = lottery % 10;int g1 = guess / 100;int g2 = (guess % 100) / 10;int g3 = guess % 10;System.out.println("Lottery is " + lottery);// Check the guessif (guess == lottery)System.out.println("Exact match: you win $10,000");else if (g1 == l1 && g3 == l2 && g2 == l3 ||g2 == l1 && g1 == l2 && g3 == l3 ||g2 == l1 && g3 == l2 && g1 == l3 ||g3 == l1 && g1 == l2 && g2 == l3 ||g3 == l1 && g2 == l2 && g1 == l3)System.out.println("Match all digits: you win $3,000"); else if (g1 == l1 || g1 == l2 || g1 == l3 ||g2 == l1 || g2 == l2 || g2 == l3 ||g3 == l1 || g3 == l2 || g3 == l3)System.out.println("Match one digit: you win $1,000"); elseSystem.out.println("Sorry, no match");}}3.16public class Exercise03_16 {public static void main(String[] args) {double x = Math.random() * 100 - 50;double y = Math.random() * 200 - 100;System.out.println(x + ", " + y);}}3.17public class Exercise03_17 {public static void main(String[] args) {// Generate scissor, rock, paperint computerNumber = (int)(Math.random() * 3);// Prompt the user to enter scissor, rock, or paper java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("scissor (0), rock (1), paper (2): ");int userNumber = input.nextInt();// Check the guessswitch (computerNumber) {case 0:if (userNumber == 0)System.out.print("The computer is scissor. You are scissor too. It is a draw");else if (userNumber == 1)System.out.print("The computer is scissor. You are rock. You won");else if (userNumber == 2)System.out.print("The computer is scissor. You are paper. You lost");break;case 1:if (userNumber == 0)System.out.print("The computer is rock. You are scissor. You lost");else if (userNumber == 1)System.out.print("The computer is rock. You are rock too. It is a draw");else if (userNumber == 2)System.out.print("The computer is rock. You are paper. You won");break;case 2:if (userNumber == 0)System.out.print("The computer is paper. You are scissor. You won");else if (userNumber == 1)System.out.print("The computer is paper. You are rock. You lost");else if (userNumber == 2)System.out.print("The computer is paper. You are paper too.It is a draw");break;}}}3.18public class Exercise03_18 {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter package weight: ");double w = input.nextDouble();if (w <= 1) {System.out.println("The shipping cost is $3.5");}else if (w <= 3) {System.out.println("The shipping cost is $5.5");}else if (w <= 10) {System.out.println("The shipping cost is $8.5");}else if (w <= 20) {System.out.println("The shipping cost is $10.5");}else {System.out.println("The package cannot be shipped");}}}3.19public class Exercise03_19 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three edgesSystem.out.print("Enter three edges (length in double): ");double edge1 = input.nextDouble();double edge2 = input.nextDouble();double edge3 = input.nextDouble();。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

3.5 跳转语句
• 3.5 跳转语句(break、continue)
– 1、break语句:用在switch条件语句和循环语 句中,它的作用是终止某个case并跳出switch 结构。 – 2、continue语句:用在循环语句中,它的作用 是终止本次循环,执行下一次循环
3.5 跳转语句
• 3.5跳转语句(break、continue)
3.3 循环结构语句
实现语句 • while语句 • do while语句 • for 语句
3.3 循环结构语句
• 3.3.1 while循环语句
– while循环语句的格式和执行流程如下图所示。
While(循环条件){ 循环体语句; }
– 例:打印1~10之间的自然数,如例2-12所示。
3.3 循环结构语句
等价于
3.2 分支结构
• 3.2.1 if条件语句
3、if…else if…else多分支语句 If…else if …else语句用于对多个条件进行判断, 进行多种不同的处理,其语法格式如下所示:
3.2 分支结构
• 3.2.1 if条件语句
– if…else if…else语句的执行流程如下图所示:
3.2 分支结构
• 3.2.1 if条件语句
– if条件语句分为三种语法格式,具体如下: – (1)if语句 – (2)if…else语句 – (3)if…else if…else语句 – 由于这三种语法格式都有自身的特点,因此, 接下来将针对这三种格式进行详细地讲解。
3.2 分支结构
• 3.2.1 if条件语句
true
执行语句1 判断条件1
false true
判断条件2
false
执行语句2
true ...... false
......
true
判断条件n
false
执行语句n 执行语句n+1
– 例:实现对学生考试成绩等价划分的程序,如
例2-9所示。
3.2 分支结构
• 3.2.2 switch条件语句
– switch语句也是一种很常见的选择语句。和if条件语句不同,它只 能针对某个表达式的值做出判断,从而决定执行哪一段代码。 – 在switch语句中,使用switch
分支结构
循环结构
3.1 程序的三种结构
• 顺序结构:程序中的语句按出现的先后顺 序依次执行,每条语句被执行且只执行一 次 • 分支结构(选择结构):程序中的语句按 条件有选择地执行,且只执行一次 • 循环结构:程序中的语句被反复多次执行
3.1 顺序结构
3.2 分支结构
实现语句
− if 语句 − switch语句
1、if单分支: if语句是指如果满足某种条件,就进行某种处理,
其语法格式如下所示:
判断条件 true 执行语句 false
上述语法格式中,判断条件是一个布尔值,当值 为true时,才会执行{}中的语句。
3.2 分支结构
• 3.2.2 if条件语句
2、if…else双分支 If…else语句是指如果满足某种条件,就进行某 种处理,否则就进行另一种处理,其语法格式如 下所示:
3.3 循环结构语句
• 3.3.1 while循环语句
– while语句和if条件语句有点类似,都是根据条 件判断来决定是否执行后面的代码,区别在于, while循环语句会反复地进行条件判断,只要条 件成立,{}内的执行语句就会执行,直到条件 不成立,while循环结束。 – While循环语句的语法结构如下所示:
3.4 循环的嵌套
• 3.4 循环嵌套
– 例:实现使用“*”打印直角三角形,具体代码如例2-15所示。
3.5 跳转语句
• 3.5 跳转语句(break、continue)
– 跳转语句用于实现循环执行过程中程序流程的 跳转,在Java中的跳转语句有break语句和 continue语句 – 1、break语句:用在switch条件语句和循环语 句中,它的作用是终止某个case并跳出switch 结构。 – 2、continue语句:用在循环语句中,它的作用
– 当break语句出现在嵌套循环的内层时,它只能 跳出内层循环,如果想跳出外层循环,则需要 对外层循环添加标记。 – 例:break的应用。 – 例:continue语句的应用。
本章小结
• 本章主要介绍了学习Java条件选择结构语句和循环结构语句的概念和 使用。 • 通过本章的学习,能够掌握几种流程控制语句的使用等。
3.2 分支结构
• 3.2.2 switch条件语句
– 需要注意的是,在switch语句中的表达式只能是byte、short、 char、int、枚举(JDK1.5引入的)、String类型(JDK1.7引入的) 的值,如果传入其他值,程序会报错。 – 通过一个案例演示根据数字来输出中文格式的星期,如例2-10所 示。
关键字来描述一个表达式,使
用case关键字来描述和表达式 结果比较的目标值,当表达式
的值和某个目标值匹配时,会
执行对应case下的语句, switch语句的基本语法结果如
右所示。
3.2 分支结构
• 3.2.2 switch条件语句
– 例如,在程序中使用数字1~7表示周一到周日, 如果想根据某个输入的数字输出中文格式的星 期值,可以通过右边所示的代码来实现。
3.2 分支结构
• 3.2.2 switch条件语句
– 在使用switch语句的过程中,如果多个case条 件后面的执行语句是一样的,则该执行语句只 需书写一次即可。 – 例如,要判断一周中的某一天是否为工作日, 同样使用数字1~7来表示星期一到星期天,当 输入的数字为1、2、3、4、5时就视为工作日, 否则就视为休息日。 – 通过一个案例来实现上面描述的情况,如例211所示。。
判断条件
true
执行语句 1
false
执行语句 2
3.2 分支结构
• 2.4.1 if条件语句
例:实现判断奇偶数的程序。
多学一招
– 在Java中有一种特殊的运算叫做三元运算,它 和if-else语句类似,语法如下: – 三元运算通常用于对某个变量进行赋值,当判 断条件成立时,运算结果为表达式1的值,否 则结果为表达式2的值。
3.3 循环结构语句
• 3.3.3 for循环语句
– 如果用①表示初始化表达式、②表示循环条件、 ③表示操作表达式、④表示循环体,则for循环 的执行流程如下所示:
– 例:实现对自然数1~10进行求和。
3.4 循环的嵌套
• 循环嵌套
– 嵌套循环是指在一个循环语句的循环体中再定 义一个循环语句的语法结构。while、 do…while、for循环语句都可以进行嵌套,并 且它们之间也可以互相嵌套,如最常见的在for 循环中嵌套for循环,格式如下:
• 3.3.2 do while循环语句
– do…while循环语句格式和执行流程如下所示:
– 例:打印1~10之间的自然数
3.3 循环结构语句
• 3.3.3 for循环语句
– for循环语句是最常用的循环语句,一般用在循 环次数已知的情况下,其语法格式如下所示:
– 在上述语法格式中,for后面的()中包括三部 分内容,初始化表达式、循环条件和操作表达 式,它们之间用“;”分隔,{}中的执行语句为循 环体

让IT教学更简单,让IT学习更有效
第3章 结构化编程
• 程序的三种结构 • Java中的循环结构
• Java中方法的分支结构 • Java中循环嵌套
✎ 学习目标
1
让IT教学更简单,让IT学习更有效
掌握程序的三种结构及实现语句
掌握
了解Java代码的
3
基本格式熟悉各种结构的序2 目录程序的三种结构
相关文档
最新文档