Introduction to Java Programming - Chapter 3: Control Statements
Formatting Output
Use the new JDK 1.5 printf statement.
System.out.printf(format, items);
Where format is a string that may consist of substrings and
format specifiers. A format specifier specifies how an item
should be displayed. An item may be a numeric value,
character, boolean value, or a string. Each specifier begins
with a percent sign
20 trang |
Chia sẻ: dntpro1256 | Lượt xem: 660 | Lượt tải: 0
Bạn đang xem nội dung tài liệu Introduction to Java Programming - Chapter 3: Control Statements, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1
Chapter 3 Control Statements
Chapter 1 Introduction to Computers, Programs,
and Java
Chapter 2 Primitive Data Types and Operations
Chapter 4 Loops
Chapter 6 Arrays
Chapter 5 Methods
Basic computer skills such as using Windows,
Internet Explorer, and Microsoft Word
§§19.1-19.3 in Chapter 19 Recursion
Chapter 23 Algorithm Efficiency and Sorting
Chapter 3 Selection Statements
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 2
Objectives
To declare boolean type and write Boolean expressions (§3.2).
To distinguish between conditional and unconditional && and ||
operators (§3.2.1).
To use Boolean expressions to control selection statements (§3.3-
3.5).
To implement selection control using if and nested if statements
(§3.3).
To implement selection control using switch statements (§3.4).
To write expressions using the conditional operator (§3.5) .
To display formatted output using the System.out.printf method
and to format strings using the String.format method (§3.6).
To know the rules governing operand evaluation order, operator
precedence, and operator associativity (§§3.7-3.8) .
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 3
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);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 4
Comparison Operators
Operator Name
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 5
Boolean Operators
Operator Name
! not
&& and
|| or
^ exclusive or
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 6
Examples
System.out.println("Is " + num + " divisible by 2 and 3? " +
((num % 2 == 0) && (num % 3 == 0)));
System.out.println("Is " + num + " divisible by 2 or 3? " +
((num % 2 == 0) || (num % 3 == 0)));
System.out.println("Is " + num +
" divisible by 2 or 3, but not both? " +
((num % 2 == 0) ^ (num % 3 == 0)));
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 7
Example: Determining Leap Year?
LeapYear Run
This program first prompts the user to enter a year as
an int value and checks if it is a leap year.
A year is a leap year if it is divisible by 4 but not by
100, or it is divisible by 400.
(year % 4 == 0 && year % 100 != 0) || (year % 400
== 0)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 8
Example: A Simple Math Learning Tool
AdditionTutor 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, as shown below. After the student types the answer in the
input dialog box, the program displays a message dialog box to
indicate whether the answer is true or false.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 9
The & and | Operators
&&: conditional AND operator
&: unconditional AND operator
||: conditional OR operator
|: unconditional OR operator
exp1 && exp2
(1 < x) && (x < 100)
(1 < x) & (x < 100)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 10
The & and | Operators
If x is 1, what is x after this
expression?
(x > 1) & (x++ < 10)
If x is 1, what is x after this
expression?
(1 > x) && ( 1 > x++)
How about (1 == x) | (10 > x++)?
(1 == x) || (10 > x++)?
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 11
Selection Statements
if Statements
switch Statements
Conditional Operators
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 12
TIP
if (number % 2 == 0)
even = true;
else
even = false;
(a)
Equivalent
boolean even
= number % 2 == 0;
(b)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 13
CAUTION
if (even == true)
System.out.println(
"It is even.");
(a)
Equivalent if (even)
System.out.println(
"It is even.");
(b)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 14
Example: An Improved Math 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, as shown in the figure.
After the student types the answer in the input dialog box,
the program displays a message dialog box to indicate
whether the answer is correct, as shown in figure.
SubtractionTutor Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 15
Example: Guessing Birth Date
GuessBirthDate Run
The program can guess your birth date. Run
to see how it works.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 16
Conditional Operator, cont.
(booleanExp) ? exp1 : exp2
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 17
Formatting Output
Use the new JDK 1.5 printf statement.
System.out.printf(format, items);
Where format is a string that may consist of substrings and
format specifiers. A format specifier specifies how an item
should be displayed. An item may be a numeric value,
character, boolean value, or a string. Each specifier begins
with a percent sign.
JDK 1.5
Feature
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 18
Frequently-Used Specifiers JDK 1.5 Feature
Specifier Output Example
%b a boolean value true or false
%c a character 'a'
%d a decimal integer 200
%f a floating-point number 45.460000
%e a number in standard scientific notation 4.556000e+01
%s a string "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
items
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 19
Creating Formatted Strings
System.out.printf(format, item1, item2, ..., itemk)
String.format(format, item1, item2, ..., itemk)
String s = String.format("count is %d and amount is %f", 5, 45.56));
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6 20
Operator Precedence
var++, var--
+, - (Unary plus and minus), ++var,--var
(type) Casting
! (Not)
*, /, % (Multiplication, division, and remainder)
+, - (Binary addition and subtraction)
, >= (Comparison)
==, !=; (Equality)
& (Unconditional AND)
^ (Exclusive OR)
| (Unconditional OR)
&& (Conditional AND) Short-circuit AND
|| (Conditional OR) Short-circuit OR
=, +=, -=, *=, /=, %= (Assignment operator)
Các file đính kèm theo tài liệu này:
- introduction_to_java_programming_chapter3_9257_1811660.pdf