java 课后答案04review
更新时间:2024-07-11 09:57:01 阅读量: 综合文库 文档下载
- java推荐度:
- 相关推荐
Chapter 4 Loops
1. count < 100 always true at Point A. count < 100 always false at Point C. count < 100 is sometimes true or sometimes false at Point B.
3. (a) Infinite number of times.
(b) Infinite number of times.
(c) The loop body is executed nine times. The printout is 3, 5, 7, 9 on
separate lines.
4. The difference between a do-while loop and a while loop is the order of evaluating the continuation-condition and executing the loop body. In a while loop, the continuation-condition is checked and then, if true, the loop body is executed. In a do-while loop, the loop body is executed for the first time before the continuation-condition is evaluated.
int sum = 0; int number; do { number = input.nextInt(); sum += number; } while (number != 0);
It would be wrong if it is initialized to a value between 0 and 100, because it could be the number you attempt to guess.
2.
5. 6.
Same. When the i++ and ++i are used in isolation, their effects are same. The three parts in a for loop control are as follows:
The first part initializes the control variable.
The second part is a Boolean expression that determines whether the loop will repeat.
The third part is the adjustment statement, which adjusts the control variable.
for (int i=1; i<=100; i++) System.out.println(i);
7.
max is 5 number 0
8.
sum is 14 count is 5
9.
max is 5 number 0
12.
13.
while loop:
long sum = 0; int i=0;
while (i<=1000) { sum += i++; }
do-while loop: long sum = 0; int i = 0; do {
sum += i++; }
while (i <= 1000);
No. Try n1 = 3 and n2 =3.
The keyword break is used to exit the current loop. The program in (A) will terminate. The output is Balance is 1.
The keyword continue causes the rest of the loop body to be skipped for the current iteration. The while loop will not terminate in (B). Yes.
for (int i=1; sum < 10000; i++) sum = sum + i;
Yes. The advantages of for loops are simplicity and readability. Compilers can produce more efficient code for the for loop than for the corresponding while loop.
10. 11.
The loop keeps doing something indefinitely. No. The scope of the variable is inside the loop.
14.
15.
16.
17. If a continue statement is executed inside a for loop, the rest of the iteration is skipped, then the action-after-each-iteration is performed and the loop-continuation-condition is checked. If a continue statement is executed inside a while loop, the rest of the iteration is skipped, then the loop-continuation-condition is checked.
Here is the fix:
int i = 0;
while (i < 4) {
if (i % 3 == 0) { i++;
continue; }
sum += i; i++; }
18. class TestBreak {
public static void main(string[]args) { int sum = 0; int number = 0;
while (number < 20 && sum < 100) { number++;
sum += number; }
System.out.println(\ } }
class TestContinue {
public static void main(String[] args) { int sum = 0; int number = 0;
while (number < 20) { number++;
if (number != 10 && number != 11) sum += number; }
System.out.println(\ } }
19. After the break statement is executed, the last println statement is executed. The output of this fragment is
1
2 1 2 2 3
20. After the continue statement is executed, the statement
j++
will be executed. The output of this fragment is
1 2 1 2 2 3
21.
Line 2: missing static.
Line 3: The semicolon (;) at the end of the for loop heading should be removed. Line 4: sum not defined.
Line 6: the semicolon (;) at the end of the if statement should be removed. Line 6: j not defined.
Line 7: Missing a semicolon for the first println statement.
Line 11: The semicolon (;) at the end of the while heading should be removed. Line 18: Missing a semicolon at the end of the do-while loop.
22.
for (int i = 0; i < 10; i++);
23.
Tip for tracing programs:
Draw a table to see how variables change in the program. Consider (a) for example.
i j output 1 0 0 1 1
2 0 0 2 1 1 2 2 3 0 0 3 1 1 3 2 2
(B) Line 3: The ; at the end of for loop should be removed. (A) compile error: i is not initialized.
3 3 4 0 0 4 1 1 4 2 2 4 3 3 4 4
(A).
0 0 1 0 1 2 0 1 2 3
(B).
**** **** 2 **** 3 2 **** 4 3 2 ****
(C).
1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx
1xxx2xxx4xxx 1xxx2xxx
1xxx
(D).
1G 1G3G 1G3G5G 1G3G5G7G 1G3G5G7G9G
24.
x is -2147483648
The reason:
When a variable is assigned a value that is too large (in size) to be stored, it causes overflow.
2147483647 + 1 is actually -2147483648
25.
(A)
n times (B)
n+1 times
(C)
n-5 times (D)
The ceiling of (n-5)/3 times
正在阅读:
java 课后答案04review07-11
制程失效模式及效应分析01-07
高考地理90分系列资料之5 高考试题中常见的地理规律和原理03-08
桂家小学2013年春季德育工作计划12-23
地质时代 - 图文01-28
植物地理学复习题06-01
电影《王牌特工:特工学院》中的西方文化透视及对英语教学的启示01-20
入门级试题答案05-06
PPP项目违约风险案例分析06-29
开掘副矿长岗位规范与标准 - 图文04-28
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 课后
- 04review
- 答案
- java
- 奥因光触媒
- 人教版2018小学六年级下册数学教学计划及进度表
- 新人教版七年级下册《相交线与平行线》单元测试题及答案
- 青岛版四年级上册五单元(除数是两位数的除法)
- 学营销的看下,听完这课程成就你的沟通天才梦想!
- 某双层扁平椭球网壳的静力分析和强震作用下的动力分析 - 图文
- 职业生涯规划书(二)
- 新人教版七年级下册数学第5章相交线与平行线单元检测题
- 融资融券题目 20110827
- 张拉压浆作业指导书
- 生物化学与分子生物学重点掌握内容
- 电气工程自动化毕业论文(住宅小区配电设计含电气图、预算)
- 兰州大学章程
- 镇江第一中学预防食物中毒管理措施
- 传热学第四版课后思考题答案(杨世铭-陶文铨)]
- 2009-2010学年度山东省招远第一学期初三年级第二学段测评化学试
- 小班科学《生蛋熟蛋大比拼》活动反思
- 孙金龙同志在调研合肥新桥国际机场建设时的讲话
- (5月28日起实施)《企业会计准则第42号——持有待售的非流动资产
- 中国电信在线计费系统(OCS)OCP接口规范(1.04)