java语言程序设计基础篇(第八版)课件PPT第三章--机械工业出版报社--李娜译.ppt
《java语言程序设计基础篇(第八版)课件PPT第三章--机械工业出版报社--李娜译.ppt》由会员分享,可在线阅读,更多相关《java语言程序设计基础篇(第八版)课件PPT第三章--机械工业出版报社--李娜译.ppt(70页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、2MotivationsIf you assigned a negative value for radius in Listing 2.1, ComputeArea.java, the program would print an invalid result. If the radius is negative, you dont want the program to compute the area. How can you deal with this situation? Liang, Introduction to Java Programming, Eighth Edition
2、, (c) 2011 Pearson Education, Inc. All rights reserved. 01321308073ObjectivesFTo declare boolean type and write Boolean expressions using comparison operators (3.2).FTo program AdditionQuiz using Boolean expressions (3.3).FTo implement selection control using one-way if statements (3.4)FTo program t
3、he GuessBirthday game using one-way if statements (3.5).FTo implement selection control using two-way if statements (3.6).FTo implement selection control using nested if statements (3.7).FTo avoid common errors in if statements (3.8).FTo program using selection statements for a variety of examples (
4、BMI, ComputeTax, SubtractionQuiz) (3.9-3.11).FTo generate random numbers using the Math.random() method (3.9).FTo combine conditions using logical operators (&, |, and !) (3.12).FTo program using selection statements with combined conditions (LeapYear, Lottery) (3.13-3.14).FTo implement selection co
5、ntrol using switch statements (3.15).FTo write expressions using the conditional operator (3.16). FTo format output using the System.out.printf method and to format strings using the String.format method (3.17). FTo examine the rules governing operator precedence and associativity (3.18). F(GUI) To
6、get user confirmation using confirmation dialogs (3.19).4The boolean Type and OperatorsOften in a program you need to compare two values, such as whether i is greater than j. Java provides six comparison operators (also known as relational operators) that can be used to compare two values. The resul
7、t of the comparison is a Boolean value: true or false. boolean b = (1 2); 5Comparison OperatorsOperator Nameless thangreater than=greater than or equal to=equal to!=not equal to6Problem: A Simple Math Learning ToolThis example creates a program to let a first grader practice additions. The program r
8、andomly generates two single-digit integers number1 and number2 and displays a question such as “What is 7 + 9?” to the student. After the student types the answer, the program displays a message to indicate whether the answer is true or false.7One-way if Statements Boolean Expression true Statement
9、(s) false (radius = 0) true area = radius * radius * PI; System.out.println(The area for the circle of + radius + radius + is + area); false (A) (B) 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);8Note if i 0 System.out.println(i is positive); (a) Wrong (b) Correct if (i 0) System.out.println(i is positive); if (i 0) System.out.println(i is positive); (a) Equivalent (b) if (i 0) System.out.println(i is positive); 9Simple if DemoWrite a program that prompts the user to enter an intege
11、r. If the number is a multiple of 5, print HiFive. If the number is divisible by 2, print HiEven.10Problem: Guessing BirthdayRunThe program can guess your birth date. Run to see how it works. 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Set1 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 Set2 1 3
12、5 7 9 11 13 15 17 19 21 23 25 27 29 31 Set3 2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 Set4 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 Set5 + = 19 11Mathematics Basis for the Game19 is 10011 in binary. 7 is 111 in binary. 23 is 11101 in binary 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Set1 8
13、 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 Set2 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 Set3 2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 Set4 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 Set5 + = 19 10000 10 + 1 10011 00110 10 + 1 00111 19 7 10000 1000 100+ 1 11101 23 12The Two-way if Statementi
14、f (boolean-expression) statement(s)-for-the-true-case;else statement(s)-for-the-false-case; Boolean Expression false true Statement(s) for the false case Statement(s) for the true case 13if.else Exampleif (radius = 0) area = radius * radius * 3.14159; System.out.println(The area for the “ + “circle
15、of radius + radius + is + area);else System.out.println(Negative input);14Multiple Alternative if Statements 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; Equivalent if (score = 90.0) grade = A; else
16、 if (score = 80.0) grade = B; else if (score = 70.0) grade = C; else if (score = 60.0) grade = D; else grade = F; 15Trace if-else statementif (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
17、.0The condition is falseanimation16Trace if-else statementif (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.0The condition is falseanimation17Trace if-else statementif (score = 90.0) grade
18、 = 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.0The condition is trueanimation18Trace if-else statementif (score = 90.0) grade = A;else if (score = 80.0) grade = B;else if (score = 70.0) grade = C;else if (sc
19、ore = 60.0) grade = D;else grade = F;Suppose score is 70.0grade is Canimation19Trace if-else statementif (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.0Exit the if statementanimation20Not
20、eThe else clause matches the most recent if clause in the same block. int i = 1; int j = 2; int k = 3; if (i j) if (i k) System.out.println(A); else System.out.println(B); (a) Equivalent (b) int i = 1; int j = 2; int k = 3; if (i j) if (i k) System.out.println(A); else System.out.println(B); 21Note,
21、 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) if (i k) System.out.println(A); else System.out.println(B);This statement prints B.22Common ErrorsAdding a semicolon
22、at the end of an if clause is a common mistake.if (radius = 0); area = radius*radius*PI; System.out.println( The area for the circle of radius + radius + is + area);This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. This error often occurs wh
23、en you use the next-line block style.Wrong23TIP if (number % 2 = 0) even = true; else even = false; (a) Equivalent boolean even = number % 2 = 0; (b) 24CAUTION if (even = true) System.out.println( It is even.); (a) Equivalent if (even) System.out.println( It is even.); (b) 25Problem: An Improved Mat
24、h Learning Tool This example creates a program to teach a first grade child how to learn subtractions. The program randomly generates two single-digit integers number1 and number2 with number1 number2 and displays a question such as “What is 9 2?” to the student. After the student types the answer i
25、n the input dialog box, the program displays a message dialog box to indicate whether the answer is correct.Run26Problem: Body Mass Index Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meter
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 语言程序设计 基础 第八 课件 PPT 第三 机械工业 出版 报社 李娜译
限制150内