
选择第3章Liang,Introduction to Java Programming,Eighth Edition, (c)2011 PearsonEducation, Inc.All rights reserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 第3章 选 择

引言如果给程序清单2.1ComputeArea.java中的radius赋一个负值,那么程序就会打印出一个非法结果。如果半径为负,那你肯定是不希望计算面积的。如何处理这个问题呢?Liang.Introduction to JavaProgramming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 2 引言 如果给程序清单2.1 ComputeArea.java中的radius 赋一个负值,那么程序就会打印出一个非法结 果。如果半径为负,那你肯定是不希望计算面 积的。如何处理这个问题呢?

学习目标声明boolean类型以及使用比较运算符书写布尔表达式(第3.2节)使用布尔表达式编写程序AdditionQuiz(第3.3节)使用单向if语句实现选择控制(第3.4节)。使用单向if语句编写游戏GuessBirthday的程序(第3.5节)使用双向if语句实现选择控制(第3.6节)。使用嵌套的if语句实现选择控制(第3.7节)。避免f语句中的常见错误(第3.8节)使用选择语句编程的不同种类的例子(BMI,ComputeTax,SubtractionOuiz)(第3.9-3.11节)。使用Math.randomQ方法产生随机数(第3.9节)。使用逻辑运算符(&&、和!)对条件进行组合(第3.12)使用带组合条件的选择语句进行编程(LeapYear、Lottery)(第3.13-3.14节)使用switch语句实现选择控制(第3.15节)。使用条件运算符书写表达式(第3.16节)。使用System.out.printf方法格式化输出和使用String.format方法格式化输出字符串(第3.17节)检查控制运算符优先级和结合方向的规则(第3.18节)。(GUI)使用确认对话框获取用户的确认信息(第3.19节)Liang,Introduction to JavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.All rights reserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 学习目标 声明 boolean类型以及使用比较运算符书写布尔表达式(第3.2节)。 使用布尔表达式编写程序 AdditionQuiz (第3.3节)。 使用单向if 语句实现选择控制 (第3.4节)。 使用单向if 语句编写游戏 GuessBirthday 的程序(第3.5节)。 使用双向if 语句实现选择控制(第3.6节)。 使用嵌套的if 语句实现选择控制(第3.7节)。 避免 if 语句中的常见错误 (第3.8节) 使用选择语句编程的不同种类的例子(BMI, ComputeTax, SubtractionQuiz)(第3.9-3.11节)。 使用 Math.random() 方法产生随机数(第3.9节)。 使用逻辑运算符 (&&、||和 !)对条件进行组合(第3.12)。 使用带组合条件的选择语句进行编程(LeapYear、Lottery)(第3.13-3.14节)。 使用switch 语句实现选择控制 (第3.15节)。 使用条件运算符书写表达式 (第3.16节)。 使用 System.out.printf方法格式化输出和使用 String.format 方法格式化输出字符串 (第3.17节)。 检查控制运算符优先级和结合方向的规则 (第3.18节)。 (GUI)使用确认对话框获取用户的确认信息 (第3.19节)

格式化控制台输出使用printf语句。System.out.printf(format, items);这里的format是指多个子串和格式标识符构成的字符串。格式标识符指定每个条目应该如何显示。这里的条目可以是数值、字符、布尔值或字符串。每个标识符都以百分号(%)开头。Liang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 4 格式化控制台输出 使用 printf 语句。 System.out.printf(format, items); 这里的format是指多个子串和格式标识符构成的字符 串。格式标识符指定每个条目应该如何显示。这里的 条目可以是数值、字符、布尔值或字符串。每个标识 符都以百分号(%)开头

常用的标识符举例输出标识符布尔值%bfalseortrue字符%C"a!十进制整数%d200浮点数%f45.460000%e标准科学计数法形式的数4.556000e+01字符串%s"Javais cool"int count = 5;条目doubleamount=45.56;&fSystem.out.printf("count is %d and amount is countamount1displaycount is 5and amount is 45.560000Liang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 5 常用的标识符 标识符 输出 举例 %b 布尔值 true or false %c 字符 'a' %d 十进制整数 200 %f 浮点数 45.460000 %e 标准科学计数法形式的数 4.556000e+01 %s 字符串 "Java is cool" int count = 5; double amount = 45.56; System.out.printf("count is %d and amount is %f", count, amount); display count is 5 and amount is 45.560000 条目

确认对话框(GUI)int option = JOptionPane.showConfirmDialog(null, "Continue"):XSelectanoptionContinue?NoCancelYesLiang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 6 (GUI) 确认对话框 int option = JOptionPane.showConfirmDialog (null, "Continue");

问题:猜生日这个程序能猜出你的生日,运行来看看它是怎么工作的193536468259106111718La131514121414151.7101115131512132021223222023822.2722122.612528282929230329303.13031282.9301Set2Set3Set5SetlSet4RunGuessBirthDateUsingConfirmationDialogLiang,Introduction toJava Programming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 7 问题:猜生日 GuessBirthDateUsingConfirmationDialog Run 这个程序能猜出你的生日,运行来看看它 是怎么工作的。 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 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