java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译

上传人:F****n 文档编号:88204760 上传时间:2019-04-20 格式:PPT 页数:69 大小:403KB
返回 下载 相关 举报
java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译_第1页
第1页 / 共69页
java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译_第2页
第2页 / 共69页
java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译_第3页
第3页 / 共69页
java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译_第4页
第4页 / 共69页
java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译_第5页
第5页 / 共69页
点击查看更多>>
资源描述

《java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译》由会员分享,可在线阅读,更多相关《java语言程序设计基础篇(第八版)课件ppt第三章--报社--李娜译(69页珍藏版)》请在金锄头文库上搜索。

1、Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807,1,Chapter 3 Selections,2,Motivations,If you assigned a negative value for radius in Listing 2.1, ComputeArea.java, the program would print an invalid result. If the radius is neg

2、ative, you dont want the program to compute the area. How can you deal with this situation?,Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807,3,Objectives,To declare boolean type and write Boolean expressions using comparison op

3、erators (3.2). To program AdditionQuiz using Boolean expressions (3.3). To implement selection control using one-way if statements (3.4) To program the GuessBirthday game using one-way if statements (3.5). To implement selection control using two-way if statements (3.6). To implement selection contr

4、ol using nested if statements (3.7). To avoid common errors in if statements (3.8). To program using selection statements for a variety of examples (BMI, ComputeTax, SubtractionQuiz) (3.9-3.11). To generate random numbers using the Math.random() method (3.9). To combine conditions using logical oper

5、ators (&, |, and !) (3.12). To program using selection statements with combined conditions (LeapYear, Lottery) (3.13-3.14). To implement selection control using switch statements (3.15). To write expressions using the conditional operator (3.16). To format output using the System.out.printf method a

6、nd to format strings using the String.format method (3.17). To examine the rules governing operator precedence and associativity (3.18). (GUI) To get user confirmation using confirmation dialogs (3.19).,4,The boolean Type and Operators,Often in a program you need to compare two values, such as wheth

7、er i is greater than j. Java provides six comparison operators (also known as relational operators) that can be used to compare two values. The result of the comparison is a Boolean value: true or false. boolean b = (1 2);,5,Comparison Operators,Operator Name greater than = greater than or equal to

8、= equal to != not equal to,6,Problem: A Simple Math Learning Tool,AdditionQuiz,Run,This example creates a program to let a first grader practice additions. The program randomly generates two single-digit integers number1 and number2 and displays a question such as “What is 7 + 9?” to the student. Af

9、ter the student types the answer, the program displays a message to indicate whether the answer is true or false.,7,One-way if Statements,if (boolean-expression) statement(s); ,if (radius = 0) area = radius * radius * PI; System.out.println(“The area“ + “ for the circle of radius “ + radius + “ is “

10、 + area); ,8,Note,9,Simple if Demo,SimpleIfDemo,Run,Write a program that prompts the user to enter an integer. If the number is a multiple of 5, print HiFive. If the number is divisible by 2, print HiEven.,10,Problem: Guessing Birthday,GuessBirthday,The program can guess your birth date. Run to see

11、how it works.,11,Mathematics Basis for the Game,19 is 10011 in binary. 7 is 111 in binary. 23 is 11101 in binary,12,The Two-way if Statement,if (boolean-expression) statement(s)-for-the-true-case; else statement(s)-for-the-false-case; ,13,if.else Example,if (radius = 0) area = radius * radius * 3.14

12、159; System.out.println(“The area for the “ + “circle of radius “ + radius + “ is “ + area); else System.out.println(“Negative input“); ,14,Multiple Alternative if Statements,15,Trace if-else statement,if (score = 90.0) grade = A; else if (score = 80.0) grade = B; else if (score = 70.0) grade = C; e

13、lse if (score = 60.0) grade = D; else grade = F;,Suppose score is 70.0,The condition is false,animation,16,Trace if-else statement,if (score = 90.0) grade = A; else if (score = 80.0) grade = B; else if (score = 70.0) grade = C; else if (score = 60.0) grade = D; else grade = F;,Suppose score is 70.0,

14、The condition is false,animation,17,Trace if-else statement,if (score = 90.0) grade = A; else if (score = 80.0) grade = B; else if (score = 70.0) grade = C; else if (score = 60.0) grade = D; else grade = F;,Suppose score is 70.0,The condition is true,animation,18,Trace if-else statement,if (score =

15、90.0) grade = A; else if (score = 80.0) grade = B; else if (score = 70.0) grade = C; else if (score = 60.0) grade = D; else grade = F;,Suppose score is 70.0,grade is C,animation,19,Trace if-else statement,if (score = 90.0) grade = A; else if (score = 80.0) grade = B; else if (score = 70.0) grade = C

16、; else if (score = 60.0) grade = D; else grade = F;,Suppose score is 70.0,Exit the if statement,animation,20,Note,The else clause matches the most recent if clause in the same block.,21,Note, cont.,Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i j) i

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号