1. Trang chủ
  2. » Công Nghệ Thông Tin

Chapter 3 Decision Structures

99 6 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 99
Dung lượng 705,76 KB
File đính kèm Chapter 3 - Decision Structures.rar (624 KB)

Nội dung

Weekly Report Chapter 3 Decision Structures 1 Contents 1 The if Statement 2 The if else Statement 3 The if else if Statement 4 Nested if Statement 5 Logical Operators 6 Comparing String Objects 7 More.

Chapter Decision Structures Contents The if Statement The if-else Statement The if-else-if Statement Nested if Statement Logical Operators Comparing String Objects More about Variable Declaration and Scope Contents (Cont’d) The Conditional Operators The Switch Statement 10.Creating Objects with the DecimalFormat Class 11.The printf Method The if Statement  Problem:  Write a program to calculate user’s average of test scores If the average is greater than 95, the program congratulates the users on obtaining a high score The if Statement (Cont’d) The if Statement (Cont’d) The if Statement (Cont’d)  Simple decision structure logic The if Statement (Cont’d) if(BooleanExpression) statement;  The BooleanExpression must be a boolean expression    A boolean expression is either true or false If the BooleanExpression is true, the very next statement is executed Otherwise, it is skipped The statement is conditionally executed because it only executes under the condition that the expression in the parentheses is true Using Relational Operators to Form Conditions Typically, the boolean expression is formed with a relational operator (binary operator  Relational Operators (in order of precedence) Meaning > Greater < Less than >= Greater than or equal to =a c=5 c==6 b!=6 true true false false false 10 85 10 Creating Objects with the DecimalFormat class   DecimalFormat class can be used to format the appearance of floating-point numbers rounded to a specified number of decimal places In Java, a value of the double type can be displayed with as many as 15 decimal places, and a value of float type can be displayed with up to decimal places 86 10 Creating Objects with the DecimalFormat class (Cont’d) double number; number = 10.0/6; System.out.println(number); 1.666666666666667  How to control the number of decimal places that are displayed ? 87 10 Creating Objects with the DecimalFormat class (Cont’d) Using the DecimalFormat class    import java.text.DecimalFormat; Create an object of the DecimalFormat class format pattern DecimalFormat formatter; formatter = new DecimalFormat(“#0.00”); constructor Constructor    Is automatically executed To initialize the object’s attributes with appropriate data and perform any necessary setup operations 88 10 Creating Objects with the DecimalFormat class (Cont’d)   #: specifies that a digit should be displayed if it is present If there is no digit in this position, no digit should be displayed 0: specifies that a digit should be displayed in this position if it is present If there is no digit in this position, a should be displayed 89 “#0.00” 90 “000.00” 91 “#,##0.00” 92 “#0%”  Formatting numbers as percentages  Writing the % character at the last position in the format pattern This causes a number to be multiplied by 100, and the % character is appended to its end 93 “#0%” 94 The printf Method  The System.out.printf method allows you to format output in a variety of ways System.out.printf(FormatString, ArgumentList) contains text and/or special formatting specifiers is a list of zero or more additional arguments 95 Format Specifiers  %d : For a decimal integer int hours = 40; System.out.printf(“I worked %d hours this week.\n”, hours); I worked 40 hours this week int dogs = 2; int cats = 4; System.out.printf(“We have %d dogs and %d cats.\n”, dogs, cats); We have dogs and cats 96 Format Specifiers  %nd : the number should be printed in a field that is n places wide int nunber = 9; System.out.printf(“The value is %6d.\n”, number); The value is  If the field is wider than the specified width, the field width will be expanded to accommodate the value int nunber = 97654; System.out.printf(“The value is %2d.\n”, number); The value is97654 97 Format Specifiers  %f : to print a floating-point number double nunber = 1278.92; System.out.printf(“The value is %f.\n”, number); The value is 1279.920000 double nunber = 1278.92; System.out.printf(“The value is %18f.\n”, number); The value is 1279.920000; double nunber = 1278.92714; System.out.printf(“The value is %8.2f.\n”, number); The value is 1278.93 98 Format Specifiers double nunber = 1253874.92714; System.out.printf(“The value is %,.2f.\n”, number); The value is 1,253,874.93  %s : To print a string argument String name = “Ringo”; System.out.printf(“My name is %s.\n”, name); My name is Ringo String name = “Ringo”; System.out.printf(“My name is %10s.\n”, name); My name is Ringo 99 ... false, the expression on the right side will not be checked 36 The && Operator (Cont’d)  A different version of the LoanQualifier program 37 38 The || Operator x y X || y true false true false false... least $30 ,000 per year, and must have been on his or her current job for at least two years 27 Nested if Statement Input    User’s annual salary Number of years at the current job 28 29 30 Nested... with the closet previous if clause that doesn’t already have its own else clause 31 Alignment of if and else clauses 32 Logical Operators  Logical operators connect two or more relational expressions

Ngày đăng: 27/09/2022, 22:14