选择第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 条目