资源预览内容
第1页 / 共69页
第2页 / 共69页
第3页 / 共69页
第4页 / 共69页
第5页 / 共69页
第6页 / 共69页
第7页 / 共69页
第8页 / 共69页
第9页 / 共69页
第10页 / 共69页
亲,该文档总共69页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 negative, 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 operators (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 control 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 operators (&, |, 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 and 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 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 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 = 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. After 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 “ + 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 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.14159; 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; else 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,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 = 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; 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号